The code below gives me a Narrowing conversion from int to uint32_t:

typedef union RGB
{
    uint32_t Color;
    struct
    {
        unsigned char B, G, R, A;
    };
} *PRGB;



inline RGB Rgb(int R, int G, int B) {RGB Result = {((R & 255) << 16) | ((G & 255) << 8) | (B & 255)}; return Result;}

Error:

warning: narrowing conversion of '((((R & 255) << 16) | ((G << 8) & 65535)) | (B & 255))' from 'int' to 'uint32_t {aka unsigned int}' inside { } [-Wnarrowing]

What I do not understand is why it tells me ((G << 8) & 65535)).. Where in the world does the 65535 come from :S

255 is -1 in 8 bits.
65535 is -1 in 16 bits.
Your 255 in the G section is getting promoted to an integer

Instead, try shifting then ANDing to get the correct bits, using the 32 bit cast:

inline RGB Rgb(int R, int G, int B) 
        {RGB Result =   (((uint32_t)R << 16) & 0x00FF0000) | 
                        (((uint32_t)G <<  8) & 0x0000FF00) | 
                        (((uint32_t)B) & 0x000000FF);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.