<<
operator<<
operator moves the bits in an int to the left
without sign extension.
255 | 127 | 127 | 127 |
The table above shows a color that is 50% gray. The alpha channel is 255 (11111111), that is fully opaque while each of the red, green, and blue channels is set to 127 (01111111). This means the color is equal to the integer 11111111011111110111111101111111 or -8,421,505. However the integer value has little meaning here. It's the individual bytes that matter.
So how do you create these colors? It's simple really. Just initialize ints to the values you want for each of the four channels, use <<
to shift them into place, and combine them with the bitwise or operator, |
. This operator sets the bits in the result that are set in any of its operands. (A set bit is a 1 bit as opposed to a 0 bit.) For example, to create a pure blue,
int alpha = 255 << 24;
int red = 0 << 16;
int blue = 255 << 8;
int green = 0;
int pureblue = alpha | red | green | blue;
If you prefer, you can combine these on one line. For example, to create the 50% gray
int halfgray = (255 << 24) | (127 << 16) | (127 << 8) | 127;