gpt4 book ai didi

c - 位移位和整数提升?

转载 作者:太空狗 更新时间:2023-10-29 16:23:50 25 4
gpt4 key购买 nike

通常,C 要求将二元运算符的操作数提升为更高级别操作数的类型。这可以被利用来避免用冗长的转换填充代码,例如:

if (x-48U<10) ...
y = x+0ULL << 40;

等等

但是,我发现,至少对于 gcc,这种行为不适用于位移位。即

int x = 1;
unsigned long long y = x << 32ULL;

我希望右侧操作数的类型能够将左侧操作数提升为 unsigned long long,以便移位成功。但是,gcc 会打印一条警告:

warning: left shift count >= width of type

是 gcc 损坏了,还是标准对位移的类型提升规则做了一些异常(exception)?

最佳答案

所谓的普通算术转换适用于许多二元运算符,但不是所有的。例如,它们不适用于位移运算符、&&、||、逗号运算符和赋值运算符。这是位移运算符的规则:

6.5.7 ... 3 Semantics ...
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.

关于c - 位移位和整数提升?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3482262/

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