Quiz Entry - updated: 2026.07.10
How do you extract the red, green, and blue components from a 32-bit RGB color value?
Shift the channel down to the low byte, then mask with & 0xFF: red is (color >> 16) & 0xFF, green (color >> 8) & 0xFF, blue color & 0xFF.
// Color format: 0x00RRGGBB
uint32_t color = 0x1A2B3C;
// 0x1A
uint8_t red = (color >> 16) & 0xFF;
// 0x2B
uint8_t green = (color >> 8) & 0xFF;
// 0x3C
uint8_t blue = color & 0xFF;
Step by step for red:
// 00000000 00011010 00101011 00111100
color = 0x001A2B3C
// 00000000 00000000 00000000 00011010
color >> 16
// 00000000 00000000 00000000 00011010 = 0x1A
& 0xFF
Pack RGB back into 32-bit:
uint32_t color = ((uint32_t)red << 16) |
((uint32_t)green << 8) |
blue;
With alpha (ARGB format 0xAARRGGBB):
uint8_t alpha = (color >> 24) & 0xFF;
uint8_t red = (color >> 16) & 0xFF;
uint8_t green = (color >> 8) & 0xFF;
uint8_t blue = color & 0xFF;