Java中的“>>”符号是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30004456/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:57:43  来源:igfitidea点击:

What does the ">>" symbol mean in Java?

java

提问by Bad Dub

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.

我在我正在查看的一些代码中看到一行,上面写着 (12 >> 1) - 1)。我打印出该值并显示为 5。如果我将 12 更改为 5,则显示为 1。

What is the ">>" symbol doing exactly?

“>>”符号究竟是做什么的?

采纳答案by CSCH

12 is 1100in 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

回答by pathfinderelite

It's a bit-shift operator. See here.

这是一个位移运算符。见这里

回答by botismarius

>>performs arithmetic right shift.

>>执行算术右移。

For example:

例如:

12 >> 1 = 6
-12 >> 1 = -6

回答by Cristiam Mercado

Binary right shift operator. The left operand's value is moved right by the number of bits specified by the right operand. For example, A >> 2 will give 15 which is 1111 in binary.

二元右移运算符。左操作数的值向右移动右操作数指定的位数。例如, A >> 2 将给出 15,即二进制的 1111。

More information: Bitwise and Bit Shift Operators

更多信息:按位和位移运算符

回答by CSCH

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 编程语言还提供了对整数类型执行按位和位移操作的运算符。有符号左移运算符<<将位模式左移,有符号右移运算符>>将位模式右移。位模式由左边的操作数给出,右边的操作数给出要移位的位置数。无符号右移运算符 >>>将零移到最左边的位置,而最左边的位置 after>>取决于符号扩展。

(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

回答by Kartic

>>is the 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 two bits, you drop the two least significant bits.

当您右移两位时,您丢弃了两个最低有效位。

Let's say, x = 00111011

让我们说, x = 00111011

So when you do, x >> 2, it results in 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 in 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