什么(浮动)(par4 >> 16&255)/ 255.0F; 意思?

She*_*hef 4 java hex bit-manipulation

我发现了这行代码:this.red = (float)(par4 >> 16 & 255) / 255.0F;其中red已被声明为a float.

我试图了解它的作用,特别是因为完整的代码是:

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);
Run Code Online (Sandbox Code Playgroud)

所以我猜这个以某种方式使用int(par4)的不同位置来着色文本.在这种情况下par4等于553648127.

这四行是什么意思,特别是>> 16 & 25

kee*_*lar 5

带alpha通道的RGB(通常称为RGBA或aRGB)是四个字节打包成一个整数.

AAAAAAAARRRRRRRRBBBBBBBBGGGGGGGG   // the original par4, each char represents one bit.
                                   // where ARBG stands for alpha, red, blue and green bit.
Run Code Online (Sandbox Code Playgroud)

shift和运算符用于检索每个字节.例如,par4 >> 16 & 255首先将整数16位右移,使得原始第3字节位于基数,并且将255其用作掩码以仅提取一个字节.

并将par4 >> 16原始字节右移16位;

0000000000000000AAAAAAAARRRRRRRR
Run Code Online (Sandbox Code Playgroud)

最后,应用&255(00000000000000000000000011111111以位表示形式)将掩盖最后8位:

  0000000000000000AAAAAAAARRRRRRRR
& 00000000000000000000000011111111
= 000000000000000000000000RRRRRRRR
Run Code Online (Sandbox Code Playgroud)

这会给你红色字节.

  • 右移,不是左转. (2认同)