<< 在 Java 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2131444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does << mean in Java?
提问by Ants
I can't find out what <<means in Java because I can't search for it on Google - I am absolutely lost!
我无法找出<<Java 中的含义,因为我无法在 Google 上搜索它 - 我完全迷路了!
The code in question is:
有问题的代码是:
public int getRGB() {
return ((red << 16) | (green << 8) | blue);
}
It's taken from: http://java.sun.com/docs/books/tutorial/essential/concurrency/example/ImmutableRGB.java
它取自:http: //java.sun.com/docs/books/tutorial/essential/concurrency/example/ImmutableRGB.java
I would really appreciate someone telling me, thanks!
我真的很感激有人告诉我,谢谢!
回答by Itay Maman
Left shift of the bits
位左移
If red == 4 (which in binary is: 00000100) then red << 16 will insert sixteen 0-bits at its right, yielding: 000001000000000000000000 which is 262144 in decimal
如果 red == 4(二进制为:00000100),则 red << 16 将在其右侧插入 16 个 0 位,结果为:000001000000000000000000,十进制为 262144
回答by Bozho
Q. What is this?
A. An "operator"
问:这是什么?
A.“操作员”
Q. How do I get to know about operators in java?
A. Google for "Java operators"
问:我如何了解 Java 中的运算符?
A. 谷歌搜索“Java 运营商”
And the result is this:
结果是这样的:
The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
有符号左移运算符“<<”将位模式左移,有符号右移运算符“>>”将位模式右移。位模式由左边的操作数给出,右边的操作数给出要移位的位置数。无符号右移运算符“>>>”将零移到最左边的位置,而“>>”之后的最左边位置取决于符号扩展。
回答by Reverend Gonzo
Left shift a number of bits. It is equivalent to multiplying by two that many times.
左移若干位。相当于乘以二那么多次。
It's used for setting specific bits in a byte, or specific bytes in a word.
它用于设置字节中的特定位,或字中的特定字节。
回答by bertolami
its a bit shift. search for operators java, it will return you detailed explanations.
它有点变化。搜索operators java,它将为您返回详细说明。
回答by Adamski
It is the left shift operator. Here's some more information on shift operators from the Java Tutorial.
它是左移运算符。以下是Java 教程中有关移位运算符的更多信息。
In your example code the three integer values: red, green and blue will typically have values 0-255. Hence, it is possible to combine these values and represent them as a single integer by shifting the red value by 16 bits, shifting the green value by 8 bits and then performing a bitwise-OR operation to combine the values.
在您的示例代码中,三个整数值:红色、绿色和蓝色通常具有 0-255 的值。因此,可以通过将红色值移动 16 位,将绿色值移动 8 位,然后执行按位或运算来组合这些值,来组合这些值并将它们表示为单个整数。
回答by YOU
Its left shifting and convert Red, Green, Blue into 24 bit Number
它的左移并将红、绿、蓝转换为 24 位数字
回答by sud03r
Its a left bit shift
它是左移
回答by bohbian
"<<" means left shift the bits of a value.
">>" means right shift the bits of a value.
“<<”表示左移一个值的位。
“>>”表示右移一个值的位。
example:
int a = 5; //the binary value of 5 is 101
a = a << 3; //left shift 3 bits on 101, 101 000<< add 3 bits(0) on the right, become '101000'
System.out.println(a); //this will display 40, the decimal for '101000'
例如:
int a = 5; //5 的二进制值是 101
a = a << 3; //在101上左移3位,101 000<<在右边加3位(0),变成'101000'
System.out.println(a); //这将显示 40,'101000' 的十进制数
int b = 9; //the binary value of 8 is 1001
b = b >> 3; //right shift 3 bits on >>000 1001add 3 bits(0) on the left, truncate the last 3 bits on the right become '001'
System.out.println(b); //this will display 1, the decimal for '001'
整数 b = 9; //8 的二进制值是 1001
b = b >> 3; //right shift 3 bits on >>000 1 001左边加3 bits(0),截断右边最后3 bits 变成'001'
System.out.println(b); //这将显示1,'001'的小数

