Scala 中 >> 和 >>> 的区别

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

Difference between >> and >>> in Scala

scalaoperatorsbit-shift

提问by Kokizzu

Is there any difference between >> and >>> operator in Scala?

Scala 中的 >> 和 >>> 运算符之间有什么区别吗?

scala> 0x7f >>> 1
res10: Int = 63

scala> 0x7f >> 1 
res11: Int = 63

scala> 0x7f >> 4
res12: Int = 7

scala> 0x7f >>> 4
res13: Int = 7

回答by Jonathon Reinhart

The >>operator preserves the sign (sign-extends), while >>>zeroes the leftmost bits (zero-extends).

>>操作者保留符号(符号-延伸),而>>>零的最左边的位(零扩展)。

-10>>2
res0: Int = -3
-10>>>2
res1: Int = 1073741821

Try it out yourself.

自己试试吧。

This is not necessary in languages like C which has signed and unsigned types, unlike Java, which also has >>>(because it has no unsigned integers).

这在像 C 这样有有符号和无符号类型的语言中是不必要的,不像 Java,Java 也有>>>(因为它没有无符号整数)。

回答by 4lex1v

They have the same meaning as in Java.

它们与 Java 中的含义相同。

From The Java? Tutorials - Bitwise and Bit Shift Operators:

爪哇?教程 - 按位和位移运算符

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 VonC

Note: with SLIP 30(Nov. 2015), Scala might end up (in 2016? 2017?) with 4 "primitive" types to represent unsigned integers: UByte, UShort, UIntand ULong.

注:带条30(11月2015),斯卡拉最终可能会(2016年2017年?)有4个“原始”类型来表示无符号整数:UByteUShortUIntULong

That would impact Bit shifting operations on UInts and ULongs, which also illustrates the difference between >>and >>>:

这会影响UInts 和 ULongs 上的位移操作,这也说明了>>和之间的区别>>>

Shift left <<and shift logical right >>>behave in the obvious way.

The case of shift arithmetic right >>is debatable.
We argue that it should not be available on unsigned integers for two reasons:

  • First, a shift arithmetic right does not appear to have any meaning on unsigned integers. The correct arithmetic shift if >>>. Therefore, similarly to unary_-, it should not be introduced.
  • Second, existing languages that do have unsigned integer types, such as the C family, actually give different semantics to >>depending on whether it has a signed or unsigned operand:
    A >>on an unsigned operand does not sign-extend. It would be confusing to a C developer for x >> 3to sign-extend in Scala, but it would be equally confusing to a Scala developer that x >> 3not sign-extend. Therefore, we prefer to leave it out completely, and let a compiler error be raised.

If a bit-twiddling-based algorithm needs the sign-extending shift right, it is always possible to reinterpret as signed, do the operation, and reinterpret back as unsigned: (x.toInt >> 3).toUInt.

Note: the current implementation does provide >>, until we agree on this point.

左移<<和逻辑右移的>>>行为很明显。

算术右移的情况>>是有争议的。
我们认为它不应该在无符号整数上可用,原因有两个:

  • 首先,算术右移似乎对无符号整数没有任何意义。正确的算术移位如果>>>。因此,与 类似unary_-,不应引入。
  • 其次,具有无符号整数类型的现有语言,例如 C 系列,实际上>>根据它是否具有有符号或无符号操作数给出不同的语义无符号操作数上的
    A>>不进行符号扩展。x >> 3在 Scala 中进行符号扩展会让 C 开发人员感到困惑,但对于x >> 3不进行符号扩展的 Scala 开发人员来说同样会感到困惑。因此,我们更愿意将其完全排除在外,并引发编译器错误。

如果基于位旋转的算法需要符号扩展右移,则始终可以重新解释为有符号,执行操作,然后重新解释为无符号:(x.toInt >> 3).toUInt

注意:当前的实现确实提供了>>,直到我们就这一点达成一致。