Need some explanation!!!

Started by
1 comment, last by joeyg2477 19 years, 10 months ago
I am trying to learn game programming and i need someone to help me out with this code... //this builds a 16bit color value in 5.6.5 format(gren dominant move #define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11)) ok the problem i am having is understanding what (b & 31) or (g & 63) means ...i do understand that b is for blue and g is for green , but how does the statement mean 5 bits for blue and 6 bits for green.... i know that the 31 and 63 have something to do with this but how.... any help will be appreciated
Advertisement
google "bitwise operators" (or, if you''re using Visual Studio, just search for it).

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.

hey, the explanation has to do the colors on a binary level.

the & (AND) operator is used to make sure no color values go over the maximum value

the << ( Shift Left Operator ) will shift each 5 or 6 bit color value into place.


say,
byte r = 35 ( 0010 0011 )
byte g = 131 ( 1000 0011 )
byte b = 66 ( 0100 0010 )

all these color values go over the range that is possible to represent using 16 bit color. The solution is to use the & operator to make sure that they all have representable values.

so we need
r < 32
g < 64
b < 32

using the & operator

r = 35 ( 0010 0011 )
& 31 ( 0001 1111 ) =
3 ( 0000 0011 )

g = 131( 0010 0011 )
& 63 ( 0011 1111 ) =
35 ( 0010 0011 )

b = 66 ( 0100 0010 )
& 31 ( 0001 1111 ) =
2 ( 0000 0010 )

so we have a value of (3,35, 2) for our color
( they are all valid color values )

Now we can shift them into place

color = b + g << 5 + r + << 11

binary level: (each color will be 16 bits)

color = 0000 0000 0000 0010 (b)
+ 0000 0100 0110 0000 (g << 5)
+ 0001 1000 0000 0000 (r << 11)
= 0001 1100 0110 0010 (final RGB color value)

So now the 3 colors are represented in a nice 16 bit WORD or SHORT or whatever.

btw, why are you working in 16 bit color?

This topic is closed to new replies.

Advertisement