c++hexsdlbitbit-shift

How does bitshifting 0xFF to the end of an Uint32 form a complete hexadecimal code?


I've been following along to Cave of Programming's C++ for Complete Beginners on Udemy, and in lesson 67 he goes over bitshifting to output certain colour pixels in an SDL window. There's a certain segment where he left-bitshifts a series of Uint8s in a Uint32, and finally adds a 0xFF to the end of it. I'm struggling to figure out why it's at the end.

void Screen::setPixel(int x, int y, Uint8 red, Uint8 green, Uint8 blue) {
    Uint32 color = 0;
    color += red; // 00000080
    color <<= 8; // 00008000
    color += green; // 00008000
    color <<= 8; // 00800000
    color += blue; // 008000FF
    color <<= 8; // 8000FF00
    color += 0xFF; // 8000FF000xFF ???


    m_buffer[(y * SCREEN_WIDTH) + x] = color; // y * screen_width gives 800, 1600 etc pixels; essentially allows row selection

}

This is the function I'm currently working with, with the comments being my thought process following along the tutorial.

Programatically, the integers (128, 0, 255) are used for (red, green, blue), corresponding to (80,00,FF) in hexadecimal

Upon compiling the program, a colour of hex code 0xFF8000FF is displayed, which I verified by placing the same colour found online (https://convertingcolors.com/hex-color-8000FF.html) and checking it side-by-side to make sure it matched the one shown in my SDL window, but I don't understand how the 0xFF "jumps" from the end of the Uint32 to the front.

I'm having difficulty finding resources on this online as I'm not entirely sure how to phrase my question concisely.

Edit: Using the g++ compiler


Solution

  • What you assign in the SDL app is the RGBA color model

    [ R  | G  | B  | A  ]
    [ 80 | 00 | FF | FF ]
    

    What you see online, is the RGB color model, without the alpha-channel

    [ R  | G  | B  ]
    [ 80 | 00 | FF ]
    

    For example

    [ R  | G  | B  | A  ]
    [ 80 | 00 | FF | 80 ]
    

    likely gives on a white background (if I am not mistaken)

    [ R  | G  | B  ]
    [ 40 | 00 | 80 ]