& - . a b, a & b 1 , a b . x, x & 1 x x & 0 0.
Therefore, for any integer, I (32 bits long) i & 0x00ff0000will leave only the red component in RGB:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
00000000111111110000000000000000
--------------------------------
00000000xxxxxxxx0000000000000000
There are other ways to extract r, g, and b:
(rgb >> 16) & 0xff
(rgb >> 8) & 0xff
rgb & 0xff
source
share