The <<
is the left shift operator: It moves the bits in the left-hand operand to the left by the number of places in the right-hand operand.
The |
is a bitwise OR operator: It does an OR operation on each bit of the operands, returning a result where each bit is 1
if either operand had a 1
there, or 0
if neither did.
So it:
- Takes the
alpha
value and shifts it 24 bits to the left
- Takes the
red
value and shifts it 16 bits to the left
- Takes the
green
value and shifts it 8 bits to the left
- ORs the results together
So say you start out with
alpha = 106 (in binary: 00000000 00000000 00000000 01101010)
red = 255 (in binary: 00000000 00000000 00000000 11111111)
green = 15 (in binary: 00000000 00000000 00000000 00001111)
blue = 170 (in binary: 00000000 00000000 00000000 10101010)
So first it shifts the value of alpha
24 bits to the left:
alpha = 00000000 00000000 00000000 01101010
<< 24 becomes:
alpha = 01101010 00000000 00000000 00000000
Then red
16 bits left:
red = 00000000 00000000 00000000 11111111
<< 16 becomes:
red = 00000000 11111111 00000000 00000000
Then green
8 bits left:
green = 00000000 00000000 00000000 00001111
<< 9 becomes:
green = 00000000 00000000 00001111 00000000
Then it ORs them all together with blue
:
alpha = 01101010 00000000 00000000 00000000
red = 00000000 11111111 00000000 00000000
green = 00000000 00000000 00001111 00000000
blue = 00000000 00000000 00000000 10101010
-------------------------------------------
rgb = 01101010 11111111 00001111 10101010