Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In 16 bit gaming devices a color is represented by an unsigned 16 bit value that

ID: 666893 • Letter: I

Question

In 16 bit gaming devices a color is represented by an unsigned 16 bit value that holds the gradients of red, green, and blue. Each of these gradients is a 5 bit value. Red is stored in the least significant 5 bits, green in the next least significant, and then blue. The last bit is unused. Given the function header below, write a function that accepts the values of red, green, and blue, and returns the color as a single 16 bit value. You may only use the bitwise operators to calculate the result, &, |, ^, ~, <<, >> and the derivative compound assignment operators, |=, &=. Assume an unsigned short is 16 bits.

Here is the function header to get you started:

unsigned short color_packing(unsigned short red, unsigned short green, unsigned short blue)

Explanation / Answer

unsigned short color_packing(unsigned short red, unsigned short green, unsigned short blue)
{
unsigned short color = 1;
color = ((color<<16)| blue)<<5;
color = (color | green)<<5;
color = (color | red);
return color;
}