[英]What does << mean in Java?
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!我真的很感激有人告诉我,谢谢!
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
Q. What is this?问:这是什么?
A. An "operator" A.“操作员”
Q. How do I get to know about operators in java?问:如何了解 Java 中的运算符?
A. Google for "Java operators" 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.
无符号右移运算符“>>>”将零移到最左边的位置,而“>>”之后的最左边位置取决于符号扩展。
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.它用于设置字节中的特定位,或字中的特定字节。
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.在您的示例代码中,三个整数值:红色、绿色和蓝色通常具有 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.
因此,可以通过将红色值移动 16 位,将绿色值移动 8 位,然后执行按位或运算来组合这些值,来组合这些值并将它们表示为单个整数。
its a bit shift.它有点变化。 search for
operators java
, it will return you detailed explanations.搜索
operators java
,它会返回详细说明。
它是一个左移
它的左移并将红、绿、蓝转换为 24 位数字
"<<" means left shift the bits of a value. “<<”表示左移一个值的位。
">>" means right shift the bits of a value. “>>”表示右移一个值的位。
example:例子:
int a = 5; int a = 5; //the binary value of 5 is 101
//5的二进制值是101
a = a << 3; a = a << 3; //left shift 3 bits on 101, 101 000<< add 3 bits(0) on the right, become '101000'
//在101上左移3位,101 000<<在右边加3位(0),变成'101000'
System.out.println(a); System.out.println(a); //this will display 40, the decimal for '101000'
//这将显示 40,'101000' 的十进制数
int b = 9;整数 b = 9; //the binary value of 8 is 1001
//8的二进制值为1001
b = b >> 3; b = b >> 3; //right shift 3 bits on >>000 1
001
add 3 bits(0) on the left, truncate the last 3 bits on the right become '001' //在>>000 1
001
上右移3 位在左边添加3 位(0),截断右边的最后3 位成为'001'
System.out.println(b); System.out.println(b); //this will display 1, the decimal for '001'
//这将显示1,'001'的小数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.