Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does (float)(par4 >> 16 & 255) / 255.0F; mean?

I found this line of code: this.red = (float)(par4 >> 16 & 255) / 255.0F; where red has been declared as a float.

I am trying to understand what it does, especially because the full code is:

this.red = (float)(par4 >> 16 & 255) / 255.0F;
this.blue = (float)(par4 >> 8 & 255) / 255.0F;
this.green = (float)(par4 & 255) / 255.0F;
this.alpha = (float)(par4 >> 24 & 255) / 255.0F;
GL11.glColor4f(this.red, this.blue, this.green, this.alpha);

so I'm guessing this somehow uses different locations of an int (par4) to color text. par4 is equal to 553648127 in this case.

What do those four lines mean, notably the >> 16 & 25?

like image 497
Shef Avatar asked Dec 26 '22 01:12

Shef


1 Answers

RGB with alpha channel (usually known as RGBA or aRGB) are four bytes packed into one integer.

AAAAAAAARRRRRRRRBBBBBBBBGGGGGGGG   // the original par4, each char represents one bit.
                                   // where ARBG stands for alpha, red, blue and green bit.

The shift and and operator are used to retrieve each individual byte. For example, par4 >> 16 & 255 is first right-shifting the integer 16 bits such that the original 3rd byte is located at base, and the 255 is served as mask to extract only one byte.

And par4 >> 16 will right-shift the original byte 16 bits;

0000000000000000AAAAAAAARRRRRRRR

Finally, applying &255, which is 00000000000000000000000011111111 in bit-representation, will mask the last 8 bits:

  0000000000000000AAAAAAAARRRRRRRR
& 00000000000000000000000011111111
= 000000000000000000000000RRRRRRRR

This gives you the red byte.

like image 111
keelar Avatar answered Jan 09 '23 03:01

keelar