了解java操作符<&书信电报;

了解java操作符<&书信电报;,java,operators,bitwise-operators,Java,Operators,Bitwise Operators,我在进行以下工作时遇到了这一行代码: public synchronized int getRGB() { return ((red << 16) | (green << 8) | blue); } public synchronized int getRGB(){ 返回((红色这是一种位移位操作。请阅读更多信息。它会将这3个数字打包为一个整数。24位颜色通常表示为RRRRRRGGGGGGBBBBBBBBBB,每种颜色有8位值。您的代码将红色值移位16位,将绿色

我在进行以下工作时遇到了这一行代码:

public synchronized int getRGB() {
    return ((red << 16) | (green << 8) | blue);
}
public synchronized int getRGB(){

返回((红色这是一种位移位操作。请阅读更多信息。它会将这3个数字打包为一个整数。

24位颜色通常表示为RRRRRRGGGGGGBBBBBBBBBB,每种颜色有8位值。您的代码将红色值移位16位,将绿色值移位8位,并保持蓝色值未移位,然后执行逻辑OR,wh在这种情况下,ich与添加值相同。请这样考虑:

每种颜色的字节值:

Red = 00011010
Green = 10101010
Blue = 11111111
移位值变为:

Red << 16 = 
00011010 00000000 00000000
Green << 8 = 
00000000 10101010 00000000
Blue = 
00000000 00000000 11111111
这是返回的24位RGB值。

public synchronized int getRGB(){
public synchronized int getRGB() {
   return ((red << 16) | (green << 8) | blue);
}

return((红色
x请参见.Bit shift而非byte shift)我的编辑修复了“byte”与“Bit”的对比:)我更喜欢你的答案,而不是我的+1。
public synchronized int getRGB() {
   return ((red << 16) | (green << 8) | blue);
}
System.out.println(4 << 2);