#### Archived

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

# Need some explanation!!!

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

## Recommended Posts

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

##### Share on other sites
google "bitwise operators" (or, if you''re using Visual Studio, just search for it).

##### Share on other sites
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?

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633410
• Total Posts
3011723
• ### Who's Online (See full list)

There are no registered users currently online

×