views:

128

answers:

3

Hi Could someone translate this statement into pseudo-code (or just plain english)?

var c:uint = 0xFF << 24 | r << 16 | g << 8 | b;
+2  A: 

It appears to be creating a composite 32-bit color value c out of 8-bit channel values stored in the r, g, b variables, along with an alpha channel value of 0xFF (all ones), i.e. fully opaque.

Essentially, what's happening is that each separate value 0xFF, r, g, b are being shifted the appropriate amount of places (24, 16, 8, and 0) and then OR-ed together to combine them and store into a single unsigned integer variable.

tzaman
+6  A: 

This is setting up the different bytes in a four-byte word. << shifts to the left by the specified number of bits, so << 24 shifts by three bytes, << 8 by one byte, etc. So the components that are ored together would be

0xFF000000         0xFF << 24
0x00rr0000         r << 16
0x0000gg00         g << 8
0x000000bb         b

To give a final answer looking something like

0xFFrrggbb

Given the variable names this is presumably a 32-bit colour value, with a byte each for red, green and blue. The fourth component could be a transparency, or a z-value (depth), or just spare bits in case they're needed elsewhere. ;-)

Andy Mortimer
A four-bit word?
Mark Byers
Doh, thanks Mark. Fixed.
Andy Mortimer
+1  A: 

It takes four byte values and put together into a 32 bit unsigned integer.

The shift operator << is used to move the values to a specific bit position.

0xFF << 24 creates the value 0xFF000000.

r << 16 places the r value in the next eight bits: 0x00rr0000.

And so on. Or-ing them together creates the value 0xFFrrggbb.

The reason for this operations is most likely to put alpha, red, green and blue components together into a 32 bit color value.

Guffa