Color components +mask

Good day to all,

I`m stuck with bitmasking and shifting…

Here is some color RGB values stored in a 16-bit value.

RRRR RGGG GGGB BBBB

Then I could use bit masking to retrieve the color components?

What does this code do?

1
2
3
4
5
6

const unsigned short redMask   = 0xF800;
unsigned short lightGray = 0x7BEF;

unsigned short redComponent   = (lightGray & redMask) >> 11;



what does the bitwise and do with the shift? Why 11? I also does not understand that lightGray hexadecimal representation, could`t find it anywhere.

Hope someone can help, thanks in advance
Try converting the hexadecimal into binary. For example, green would be 0000011111100000

Also, it is very rare to use an actual 16-bit color representation in this current day and age.
Last edited on
0x7BEF = 0111101111101111
0XF800 = 1111100000000000

(lightGray & redMask) = 01111000000000000

(lightGray & redMask ) >>11 = 0000000000001111

If I use bitmasking to generate moves for a checker game I could still use C-style arrays to represent the board instead of std::bitset?
Topic archived. No new replies allowed.