I am learning about bit wise operators and ran into this problem:
In computer graphics, colors are often stores as three numbers, representing red, green and blue intensities. Suppose that each number requires eight bits, and we'd like to store all three values in a single long integer.
Write a macro named
MK_COLORwith three parameters (the red, green and blue intensities).MK_COLORshould return alongin which the last three bytes contain the red, green and blue intensities, with the red value as the last byte and the green value as the next-to-last byte.
The solution is:
#define MK_COLOR(r,g,b) ((long) (b) << 16 | (g) << 8 | (r))
I don't fully understand how the solution works, so I will try to break down my understanding, lets say r = 20 (10100), g = 30 (11110) and b = 40 (101000).
bis shifted left by 16 bits so we get00010100 00000000 00000000gis shifted by 8 bits so we get11110000- there is a
|OR operator which looks like this:
00010100 00000000 00000000
| 11110000 00000000 00000000
---------------------------
11110100 00000000 00000000
- the last step does OR on this result we got but with
rwhich looks like this:
11110100 00000000 00000000
| 10100000 00000000 00000000
---------------------------
11110100 00000000 00000000 // result
The result is 11110100 00000000 00000000 which is 15990784 in decimal. This result however is incorrect according to when I run the program and get 2629140 as the answer.
Why is it wrong? Could you please explain what I did wrong and how I can better my understanding of this?