Advertisement Jump to content


This topic is now archived and is closed to further replies.


Macro question

This topic is 5886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am a little confused about the macros used to define a RGB color.. I''ve seen #define _RGB16BIT555(r,g,b) ((b & 31) + ((g&31) << 5) + ((r&31) << 10)) and #define _RGB16BIT555(r,g,b) ((r << 10) | (g << 5) | (b)) most of the text/docs just list them and assumed the reader knows why. Well, I''m sorry but I dont understand why you have to shift (divide) the values, and why by the amount (10, 5, etc). Can someone kind enough to explain?

Share this post

Link to post
Share on other sites
well, the << operator is a binary shift.

To my knowlege, it shifts the byte however many places to the left. ( >> does right). this means that:

0111 1111 >> 1;

is equal to 1111 1110. (see what happened? the last bit came from nowhere and we dropped the higest bit)

1111 1110 << 1 would be: 0111 1111

Note that this is all binary.

this is just my limited knowlege, though.

Share this post

Link to post
Share on other sites
Ehm, what are you talking about? You're right that >> shifts to the right, but then you write that 0111 1111 >> 1 is 1111 1110. Clearly, you have moved the bits to the left in that example, not to the right.

What the macro does is just to pack the bits into a 16 bit value, which has the format 0rrr rrgg gggb bbbb.

First the red color is shifted to the left ten steps:
r: 0000 0000 000r rrrr
r << 10: 0rrr rr00 0000 0000

And the green color five steps:
g: 0000 0000 000g gggg
g << 5: 0000 00gg ggg0 0000

Finally, put them together with or-statements '|':
0rrr rr00 0000 0000 or
0000 00gg ggg0 0000 or
0000 0000 000b bbbb
0rrr rrgg gggb bbbb

And you have the packed rgb-value.

[edited by - aleph on December 5, 2002 2:46:10 PM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!