Why it’s so simple :L it’s like playing with legos.
0101 (decimal 5)
AND 0011 (decimal 3) // AND is useful for "masking" i.e, grabbing only specific pixels you care about
= 0001 (decimal 1)
0101 (decimal 5)
OR 0011 (decimal 3) // OR is useful for combining bits together
= 0111 (decimal 7)
bit shifting is so straightforward.
0100 >> 1 becomes 0010. (move the bits to the right 1 time)
0001 << 1 becomes 0010. (move the bits to the left 1 time)
an int is made up of 32 bits (= 4 bytes (1 byte = 8 bits (1 bit is either 0 or 1)))
The first byte of an int is “int & 0xFF” (bits are read from right to left).
The second byte of an int is either “int & 0xFF00” or “(int >> 8) & 0xFF”
ARGB pixel: 0000 0000 0000 0000 0000 0000 0000 0000
“int b = pixel & 0xFF”:
pixel 0000 0000 0000 0000 0000 0000 0000 0000 AND 0000 0000 0000 0000 0000 0000 1111 1111 ( & 0xFF ) b = 0000 0000 0000 0000 0000 0000 0000 0000
“int g = pixel & 0xFF00” or “int g = (pixel >> 8) & 0xFF”
pixel 0000 0000 0000 0000 0000 0000 0000 0000 AND 0000 0000 0000 0000 1111 1111 0000 0000 ( & 0xFF00 ) g = 0000 0000 0000 0000 0000 0000 0000 0000
or
pixel 0000 0000 0000 0000 0000 0000 0000 0000 pixel >> 8 0000 0000 0000 0000 0000 0000 0000 0000 AND 0000 0000 0000 0000 0000 0000 1111 1111 ( & 0xFF ) g = 0000 0000 0000 0000 0000 0000 0000 0000
“byte r = (pixel & 0xFF0000) >> 16”
`pixel 0000 0000 0000 0000 0000 0000 0000 0000
AND 0000 0000 1111 1111 0000 0000 0000 0000 ( & 0xFF0000 )
= 0000 0000 0000 0000 0000 0000 0000 0000
16 0000 0000 0000 0000 0000 0000 0000 0000
r = 0000 0000 (byte can only hold 8 bits)`