简体   繁体   English

Rust的移位运算符的确切语义是什么?

[英]What are the exact semantics of Rust's shift operators?

I tried to find exact information about how the << and >> operators work on integers, but I couldn't find a clear answer ( the documentation is not that great in that regard). 我试图找到有关<<>>运算符如何处理整数的确切信息,但我找不到一个明确的答案( 文档在这方面并不是那么好)。

There are two parts of the semantics that are not clear to me. 语义的两个部分对我来说并不清楚。 First, what bits are "shifted in"? 首先,哪些位“移入”?

  • Zeroes are shifted in from one side (ie 0b1110_1010u8 << 4 == 0b1010_0000u8 ), or 零从一侧移入(即0b1110_1010u8 << 4 == 0b1010_0000u8 ),或者
  • the bits rotate (ie 0b1110_1010u8 << 4 == 0b1010_1110u8 ), or 位旋转(即0b1110_1010u8 << 4 == 0b1010_1110u8 ),或者
  • it's unspecified (like overflowing behavior of integers is unspecified), or 它是未指定的(如整数的溢出行为未指定),或
  • something else. 别的。

Additionally, how does shifts work with signed integers? 此外,移位如何使用有符号整数? Is the sign bit also involved in the shift or not? 符号位是否也参与了班次? Or is this unspecified? 或者这是未指定的?

What are the exact semantics of Rust's shift operators? Rust的移位运算符的确切语义是什么?

There are none. 没有了。 The shift operators are a user-implementable trait and you can do basically anything you want in them. 移位运算符是用户可实现的特性,您可以基本上执行任何您想要的操作。 The documentation even shows an example of "[a]n implementation of Shr that spins a vector rightward by a given amount." 该文档甚至显示了一个“[a] n实现Shr的示例,该向量将向量向右旋转给定量。”


how the << and >> operators work on integers, <<>>运算符如何处理整数,

The reference has a section on Arithmetic and Logical Binary Operators . 该参考文献有一节关于算术和逻辑二元运算符 Most usefully, it contains this footnote: 最有用的是,它包含了这个脚注:

Arithmetic right shift on signed integer types, logical right shift on unsigned integer types. 有符号整数类型的算术右移,无符号整数类型的逻辑右移。

Logical shifting and arithmetic shifting are preexisting computer science terms with established definitions. 逻辑移位算术移位是预先存在的计算机科学术语和已建立的定义。

Zeroes are shifted in 零转移

Yes. 是。

the bits rotate 这些位旋转

No. There are separate methods for rotating left and right . 号有旋转不同的方法

The thin documentation on the traits Shl and Shr is intentional, so that they may adopt a behaviour that is most suitable for the type at hand (think newtypes!). 关于特征ShlShr的精简文档是有意的,因此他们可以采用最适合手头类型的行为(想想newtypes!)。

With that said, when it comes to the base integer types, the Rust reference covers how they behave, with a bit of inference: 话虽如此,当谈到基本整数类型时, Rust引用涵盖了它们的行为方式,并进行了一些推断:

  • << | << | Left Shift | 左转| std::ops::Shl

  • >> | >> | Right Shift* | 右移* | std::ops::Shr

* Arithmetic right shift on signed integer types, logical right shift on unsigned integer types. *有符号整数类型的算术右移,无符号整数类型的逻辑右移。

It also includes a few examples, which further clarifies that these are conventional logical/arithmetic shifts: zeros are inserted to the least significant bits on a left bit shift, and the most significant bit is extended for signed integers on a right bit shift. 它还包括一些例子,它们进一步阐明了这些是传统的逻辑/算术移位:在左移位时将零插入最低有效位,在右移位上为有符号整数扩展最高有效位。 It is also not a rotation , as described in the methods rotate_left and rotate_right . 也不是旋转 ,如方法rotate_leftrotate_right

assert_eq!(13 << 3, 104);
assert_eq!(-10 >> 2, -3);

Moreover, shifting too many bits may be regarded as an arithmetic overflow, and is not undefined behaviour. 此外,移位太多位可以被视为算术溢出,并且不是未定义的行为。 See: Is it expected that a too large bitshift is undefined behavior in Rust? 请参阅: 在Rust中,是否预期过大的位移是未定义的行为?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM