gpt4 book ai didi

c++ - 在 x86 机器上移动超过 32 位的 uint64_t 整数未定义行为?

转载 作者:IT老高 更新时间:2023-10-28 23:09:32 35 4
gpt4 key购买 nike

学习艰难,我尝试在 x86 机器上将 long longuint64_t 左移到 32 位以上,结果 0 .我隐约记得在某处读过比 32 位机器移位运算符仅在前 32 位上工作但无法记忆来源。我想知道在 x86 机器上移动超过 32 位的 uint64_t 整数是否是未定义行为?

最佳答案

标准说(n1570 中的 6.5.7):

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

转移 uint64_t小于 64 位的距离完全由标准定义。

自从 long long必须至少为 64 位,移位 long long如果结果不溢出,则小于 64 位的值由非负值标准定义。

但是,请注意,如果您编写适合 32 位的文字,例如uint64_t s = 1 << 32正如@drhirsch 所推测的那样,您实际上并没有移动 64 位值,而是移动 32 位值。那是未定义的行为。

最常见的结果是偏移 shift_distance % 32或 0,取决于硬件的功能(并假设编译器的编译时评估模拟硬件语义,而不是鼻恶魔。)

使用1ULL < 63 使移位操作数 unsigned long long 在转变之前

关于c++ - 在 x86 机器上移动超过 32 位的 uint64_t 整数未定义行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10499104/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com