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

查看:67
本文介绍了Rust的shift运算符的确切语义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到有关<<>>运算符如何处理整数的确切信息,但是我找不到明确的答案(

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"?

  • 从一侧(即0b1110_1010u8 << 4 == 0b1010_0000u8)或
  • 移入零
  • 这些位旋转(即0b1110_1010u8 << 4 == 0b1010_1110u8),或
  • 未指定(例如未指定整数的溢出行为),或
  • 其他的东西.
  • Zeroes are shifted in from one side (i.e. 0b1110_1010u8 << 4 == 0b1010_0000u8), or
  • the bits rotate (i.e. 0b1110_1010u8 << 4 == 0b1010_1110u8), or
  • 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?

推荐答案

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

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

没有.移位运算符是用户可实现的特征,基本上,您可以在其中执行任何所需的操作.该文档甚至显示了一个示例,"Shr的[a] n实现将向量向右旋转给定的数量."

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."

<<>>运算符如何处理整数

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.

零移了

是的

位旋转

不.旋转正确.

这篇关于Rust的shift运算符的确切语义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆