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

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

I am trying to understand what it is doing, 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 guess it somehow uses different int ( par4 ) locations for colored text. par4 in this case is 553648127 .

What do these four lines mean, especially >> 16 & 25 ?

+4
source share
4 answers

Alpha-RGB (usually called RGBA or aRGB) consists of four bytes packed into a single unit.

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

The shift and and operator is used to extract each individual byte. For example, par4 >> 16 & 255 first shifts the integer 16 bits so that the original 3rd byte is in the database, and 255 serves as a mask to extract only one byte.

And par4 >> 16 shifts the original bit to 16 bits,

 0000000000000000AAAAAAAARRRRRRRR 

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

  0000000000000000AAAAAAAARRRRRRRR & 00000000000000000000000011111111 = 000000000000000000000000RRRRRRRR 

This gives you a red byte.

+5
source

>> is the right shift bit operator. This is easier to see in binary format:

 b1000 >> 3 = b0001 

You see how he moved the bits correctly.

& is the bitwise AND operator. The result of x & y is the value of only the bits in x and y that were included.

 b1 & b1 = b1 b1 & b0 = b0 b0 & b1 = b0 b11 & b01 = b01 

So,

 this.red = (float)(par4 >> 16 & 255) / 255.0F; 

... takes a value from par4 and shifts the bit to the right by 16 bits to move the "red" value to the far right. Then it disguises everything except the rightmost byte. Then it divides the value (which should be in the range from 0 to 255 inclusive) by 255 as a float, with the result that the floating point value between 0.0 and 1 tells us how the "red" color was.

And similarly for blue and green.

He then uses the same mechanism to determine how strong the alpha channel is (transparency).

+4
source

A bit connection with 255 (0b11111111) discards all bits from an integer, with the exception of the eight least significant bits. If you change the shift first, you can access eight-bit groups from other areas of this integer.

0
source

par4 seems to define a color with an RGB value and alpha as an integer with 4 bytes.

Thus, 4 bytes are allocated by shifting the bytes of the "lower" bytes to the right with the >> operator and masking the "higher" bytes with & 255 . After that, the byte value is normalized to a floating-point value between 0.0 and 1.0 using / 255.0f .

If 4 bytes in our integer look like AARRGGBB, where AA, RR, GG, BB each denotes a byte, and you, for example, want to highlight red, you first change the green and blue bytes to >> 16 , which will leave you with the integer with the number 0000AARR, then you mask alpha with & 255 , which will leave you with the integer 00000000RR, where RR is now 1 byte (2 nibbles) with values ​​from 0 to 255, which you finally convert to float between 0.0 and 1.0 division at / 255.0f .

0
source

Source: https://habr.com/ru/post/1487665/


All Articles