gpt4 book ai didi

c - 移动 unsigned int 超过它的大小,是否未定义?

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

2011 年草案说:

6.5.7 Bitwise shift operators/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 × 2^E2, 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.

J.2 Undefined behavior An expression is shifted by a negative number or by an amount greater than or equal to the width of the promoted expression (6.5.7).

如何解释两者? J.2 是指所有移位(无符号或无符号)还是6.5.7 部分中明确提到的 UB(仅用于有符号)。

我的意思是,是 unsigned int i=...; i <<= sizeof(i)*CHAR_BIT; UB?

最佳答案

无论签名如何,您引用的那段上面的段落都说了同样的话:

6.5.7 Bitwise shift operators / 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.

所以,无论是未签名还是已签名,都是 UB。

关于c - 移动 unsigned int 超过它的大小,是否未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45432694/

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