正转向在x86机器未定义行为一uint64_t中的整数超过32位? [英] Is Shifting more than 32 bits of a uint64_t integer on an x86 machine Undefined Behavior?

查看:502
本文介绍了正转向在x86机器未定义行为一uint64_t中的整数超过32位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习困难的方式,我想留在移长长 uint64_t中到超过32位在x86机器造成 0 。我依稀记得有阅读的地方比在32位机器移位运算符只对前32位的工作,但不能回忆的源泉。
我想知道的是,如果一个转移整数uint64_t中超过32位的x86机器上是不确定的行为?

Learning the hard way, I tried to left shift a long long and uint64_t to more than 32 bits on an x86 machine resulted 0. I vaguely remember to have read somewhere than on a 32 bit machine shift operators only work on the first 32 bits but cannot recollect the source. I would like to know is if Shifting more than 32 bits of a uint64_t integer on an x86 machine is an Undefined Behavior?

推荐答案

标准说(在n1570 6.5.7):

The standard says (6.5.7 in n1570):

3的整数促销活动在每个操作数执行。的结果的类型是
  该促进左操作数。如果右操作数的值是负的或大于或等于促进左操作数的宽度,行为是理解过程网络定义。

3 The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

4 E1 1所述的结果;< E2是E1左移E2位位置;腾空位音响与LLED
  零。如果E1有一个无符号类型,则结果的值是E1×2 E2 ,降低的模
  人比在结果类型的最大值再presentable多。如果E1有一个签名
  类型和非负价值,E1×2 E2 重新presentable的结果类型,然后就是
  所得到的值;否则,行为是理解过程网络定义。

4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2 , reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

5 E1 >> E2的结果为E1右移E2位的位置。如果E1有一个无符号类型
  或者,如果E1有一个签名的类型和一个非负值,则结果的值是积分
  E1 / 2 E2 的商的一部分。如果E1有一个签名的类型和负值,所述
  得到的值是实现德网络定义。

5 The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2E2 . If E1 has a signed type and a negative value, the resulting value is implementation-defined.

移小于64比特的 uint64_t中的距离由标准完全定义

Shifting a uint64_t a distance of less than 64 bits is completely defined by the standard.

由于长长必须至少为64位,移长长值小于64位被定义为对于非负值的标准,如果结果不会溢出。

Since long long must be at least 64 bits, shifting long long values less than 64 bits is defined by the standard for nonnegative values, if the result doesn't overflow.

请注意,但是,如果你写一个适合为32位,例如文字 uint64_t中S = 1&LT;&LT; 32 按@drhirsch的推测,你实际上并没有转向64位的值,但32位之一。这是不确定的行为。最常见的结果是由 shift_distance%32 或0的转变,这取决于硬件的用途。

Note, however, that if you write a literal that fits into 32 bits, e.g. uint64_t s = 1 << 32 as surmised by @drhirsch, you don't actually shift a 64-bit value but a 32-bit one. That is undefined behaviour. The most common results are a shift by shift_distance % 32 or 0, depending on what the hardware does.

这篇关于正转向在x86机器未定义行为一uint64_t中的整数超过32位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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