This question already has an answer here:
这个问题在这里已有答案:
- What does >> do in java? 4 answers
>>在java中做什么? 4个答案
I see a line in some code I'm looking at and it says (12 >> 1) - 1). I print that value out and it comes out as a 5. If I change the 12 to 5, it comes out as a 1. What is the ">>" symbol doing exactly? Thanks
我看到一些我正在看的代码中的一行,它说(12 >> 1) - 1)。我将该值打印出来并以5的形式出现。如果我将12更改为5,则表示为1.什么是“>>”符号?谢谢
6 个解决方案
#1
12 is 1100
in binary. A right shift (>> is the bitwise right shift operator) by one bit produces
12是二进制的1100。右移((>>是按位右移运算符)一位产生
1100 -> 0110
which comes out to be 6.
这是6。
Thus we have that,
因此,我们有,
6 - 1 = 5
#2
>>
is signed right shift operator. It 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.
>>签署了右移运营商。它将位模式向右移动。位模式由左侧操作数给出,位置数由右侧操作数移位。
When you shift right 2 bits you drop the 2 least bits.
向右移2位时,丢弃2位。
Lets say, x = 00111011
可以说,x = 00111011
So when you do, x >> 2
, it results x = 00001110
所以当你这样做时,x >> 2,结果为x = 00001110
This is essentially the same thing as dividing a value by 4 or by 2 two times while dropping the fractional part.
这与将值除以小数部分时将值除以4或2两次基本相同。
So, below code will result 4
因此,下面的代码将导致4
byte val = 100;
val = (byte) (val >> 2);
System.out.println(val);
Explaining your example:
解释你的例子:
- The binary representation of 12 is: 1100
- 12 >> 1 is equivalent to 0110 which is 6 in decimal
- so (12 >> 1) - 1) is equivalent to 6-1 that is 5
12的二进制表示是:1100
12 >> 1相当于0110,即十进制的6
所以(12 >> 1) - 1)相当于6-1即5
#4
>>
performs arithmetic right shift eg:
>>执行算术右移,例如:
12 >> 1 = 6
-12 >> 1 = -6
#5
See Bitwise and Bit Shift Operators
请参见按位和位移运算符
The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. 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.Java编程语言还提供对整数类型执行按位和位移操作的运算符。带符号的左移位运算符< <向左移位位模式,带符号的右移位运算符> >将位模式向右移位。位模式由左侧操作数给出,位置数由右侧操作数移位。无符号右移运算符>>>将零移动到最左边的位置,而>>之后的最左边位置取决于符号扩展。
(12 >> 1) - 1)
>>
shifts binary 12(1100)
1 times to the right.12 >> 1 == 6(110)
>>将二进制12(1100)向右移1次。 12 >> 1 == 6(110)
6 - 1 == 5
6 - 1 == 5
#6
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. For example, A >> 2 will give 15 which is 1111.
二进制右移算子。左操作数值向右移动右操作数指定的位数。例如,A >> 2将给出15,即1111。
More info: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
更多信息:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
#1
12 is 1100
in binary. A right shift (>> is the bitwise right shift operator) by one bit produces
12是二进制的1100。右移((>>是按位右移运算符)一位产生
1100 -> 0110
which comes out to be 6.
这是6。
Thus we have that,
因此,我们有,
6 - 1 = 5
#2
>>
is signed right shift operator. It 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.
>>签署了右移运营商。它将位模式向右移动。位模式由左侧操作数给出,位置数由右侧操作数移位。
When you shift right 2 bits you drop the 2 least bits.
向右移2位时,丢弃2位。
Lets say, x = 00111011
可以说,x = 00111011
So when you do, x >> 2
, it results x = 00001110
所以当你这样做时,x >> 2,结果为x = 00001110
This is essentially the same thing as dividing a value by 4 or by 2 two times while dropping the fractional part.
这与将值除以小数部分时将值除以4或2两次基本相同。
So, below code will result 4
因此,下面的代码将导致4
byte val = 100;
val = (byte) (val >> 2);
System.out.println(val);
Explaining your example:
解释你的例子:
- The binary representation of 12 is: 1100
- 12 >> 1 is equivalent to 0110 which is 6 in decimal
- so (12 >> 1) - 1) is equivalent to 6-1 that is 5
12的二进制表示是:1100
12 >> 1相当于0110,即十进制的6
所以(12 >> 1) - 1)相当于6-1即5
#3
It's a bit-shift operater. See here
这是一个有点转变的操作者。看这里
#4
>>
performs arithmetic right shift eg:
>>执行算术右移,例如:
12 >> 1 = 6
-12 >> 1 = -6
#5
See Bitwise and Bit Shift Operators
请参见按位和位移运算符
The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. 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.Java编程语言还提供对整数类型执行按位和位移操作的运算符。带符号的左移位运算符< <向左移位位模式,带符号的右移位运算符> >将位模式向右移位。位模式由左侧操作数给出,位置数由右侧操作数移位。无符号右移运算符>>>将零移动到最左边的位置,而>>之后的最左边位置取决于符号扩展。
(12 >> 1) - 1)
>>
shifts binary 12(1100)
1 times to the right.12 >> 1 == 6(110)
>>将二进制12(1100)向右移1次。 12 >> 1 == 6(110)
6 - 1 == 5
6 - 1 == 5
#6
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. For example, A >> 2 will give 15 which is 1111.
二进制右移算子。左操作数值向右移动右操作数指定的位数。例如,A >> 2将给出15,即1111。
More info: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
更多信息:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html