In
int red = (clr & 0x00ff0000) >> 16;
The &
will zero out all bits except those that are wanted:
0x00123456 & 0x00ff0000 == 0x00120000
The bit shift will place these bits at the desired position:
0x00120000 >> 16 == 0x00000012 == 0x12
Similarly for the other two channels.
0x00123456 & 0x0000ff00 == 0x00003400
0x00003400 >> 16 == 0x34
0x00123456 & 0x000000ff == 0x56
The reason for this is that the ARGB format stuffs four bytes (alpha, red, green, blue) into one int: 0xAaRrGgBb
. The RGB format is similar except it doesn't use the alpha (opacity) channel. The whole point behind the bit-shifting is to separate those bytes: clr == 0x123456
to red == 0x12
green == 0x34
blue == 0x56
Note that each byte (8 bits) is represented by two hexadecimal digits (4 bits each) in the hexadecimal notation, so shifting by 16 bits shifts by 4*4 bits = 4 hexadecimal digits = 2 bytes.