The following macro-function takes three separate values for red, green, and blue and combines them into a single 24-bit value.

#define RGB(r,g,b) ;

(bitlshift(bitand((r),0xff),16)+bitlshift(bitand((g),0xff),8)+bitand((b),0xff))

Each value is 8 bits—BITAND( ) makes sure of that. The red value is shifted 16 bits to the left to make room for the green and blue values. The green value is shifted 8 bits to the left to make room for the blue value. All three numbers are added together to form a single 24-bit number. For example, suppose you pass the following values, shown here in binary to the macro-function:

Red 11000011

Green 10101010

Blue 11111111

Shifting the red and green results in the following values:

Red 11000011 00000000 00000000

Green 00000000 10101010 00000000

Blue 00000000 00000000 11111111

The 8-bit values are shifted so their bits do not overlap. Now, adding the values together combines them into a single 24-bit value:

RGB 11000011 10101010 11111111