Help From Bit Shifting Guru Please

Hello all,
I have an RGB lcd that displays 16 bit colour such as 0xffff (White), 0x001f (Blue), does anyone know of an easy-ish way to control the individual RGB componants of each colour, the 16 bit colour format is as follows

0b1111100000000000 (5 bits for red)
0b0000011111100000 (6 bits for green)
0b0000000000011111 (5 bits for Blue)

the bit shifting comes into it when i want to take a value of red and << it into the right position something like

int RED = 63; //Full Red
int COLOUR = RED <<  11

but i know this isn't right, also green having 6 bits complicates things
also should Colour be a long variable as it is a 16 bit number.

Im not asking for any freebies, just a few pointers would be very helpfull, the display uses the HX8347 chip, so far i haven't found an arduino library to use this and have resorted to writing my own with code intended for 8051 uc's.

Thanks in advance

It isn't just a question of shifting, you also need to mask off the bits you don't want to affect, using
bitwise AND and OR.

For a 16 bit value, an unsigned int is OK for the Arduino.

#define RED_MASK 0xF800
#define GREEN_MASK 0x07E0
#define BLUE_MASK 0X001F

If I understand your question correctly, you're looking for something like this:

int buildRGB(size_t red, size_t green, size_t blue) {
      if (red > 31 || green > 63 || blue > 31)
            return 0;
      return (red << 11) | (green << 5) | blue;
}

That should allow you to create any RGB code easily and human-readable. The error check is minimal but prevents weird errors.

With this code, red becomes:

buildRGB(31,0,0)

Thanks both,
Yes Success

void HX8347::clearBG(int red, int green, int blue)
{
  int i,j;
  address_set(0,0,239,319);
  for(i=0;i<320;i++)
    {
      for (j=0;j<240;j++)
      {
          main_Write_DATA(red << 11 | green << 5 | blue);
      }
    }            
}
glcd.clearBG(0,0,31); // Clear screen blue background

It works, Thanks again

struct rgb_t
{
    union
    {
        unsigned short      _r:5, _g:6, _b:5;
        unsigned short      _rgb;
    };
};

rgb_t           rgb;
unsigned short  color;

rgb._r  = 0x1F;     // contains: 0x1F
rgb._g  = 0x3F;     // contains: 0x3F
rgb._b  = 0x1F;     // contains: 0x1F
color   = rgb._rgb; // contains: 11111 111111 11111

rgb._r  = 0x21;     // contains: 0x01
rgb._g  = 0x4F;     // contains: 0x0F
rgb._b  = 0x23;     // contains: 0x03
color   = rgb._rgb; // contains: 00001 001111 10011

Modifying your code -

struct rgb_t
{
    union
    {
        unsigned short      _r:5, _g:6, _b:5;
        unsigned short      _rgb;
    };
};

void HX8347::clearBG(int red, int green, int blue)
{
    address_set(0, 0, 239, 319);

    rgb_t   rgb;
    rgb._r  = red;
    rgb._g  = green;
    rgb._b  = blue;

    for ( int i = 0; i < 320; i++ )
    {
        for ( int j = 0; j < 240; j++ )
        {
            main_Write_DATA(rgb._rgb);
        }
    }
}

Or alternately you should be able to use -

struct rgb_t
{
    union
    {
        unsigned short      _r:5, _g:6, _b:5;
        unsigned short      _rgb;
    };
    
    rgb_t(unsigned short r, unsigned short g, unsigned short b)
        : _r(r) _g(g) _b(b)
    {}
};

void HX8347::clearBG(int red, int green, int blue)
{
    address_set(0, 0, 239, 319);

    rgb_t   rgb(red, green, blue);

    for ( int i = 0; i < 320; i++ )
    {
        for ( int j = 0; j < 240; j++ )
        {
            main_Write_DATA(rgb._rgb);
        }
    }
}