gpt4 book ai didi

c - 负值左移 0 个位置?

转载 作者:太空狗 更新时间:2023-10-29 17:02:58 25 4
gpt4 key购买 nike

在 C 中,负值的左移是未定义的行为。我遇到过两个使用 Intel 的 ICC 编译的库,其中有问题的代码被删除了。同样的代码在 Clang、Comeau、GCC 和 MSVC 下运行良好。

标准是否提到将负值左移 0 位?它也是未定义的吗?

(我很好奇的细节是 0 大小的移位,实际上根本没有移位。所以我想知道语言是否含糊,以至于可能允许 0 大小的左移位)。

最佳答案

摘自 C99 with Technical corrigenda TC1, TC2, and TC3 included :

6.5.7 Bitwise shift operators

[...]

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

因此,始终未定义。

关于c - 负值左移 0 个位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22883790/

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