c - 位移位和整数提升?

标签 c gcc standards bit-manipulation integer-promotion

通常,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/53819217/

相关文章:

c - 希望通过 C 中等效的 ASCII 值将 int 存储在 char 数组中

c - -funsafe-math-optimizations 中包含哪些优化?

c++ - 为什么 GCC 在我的机器上创建额外的汇编指令?

gcc - "gcc -x c"和 "gcc -x c++"汇编输出的区别

node.js - 三元运算符不应在 Node.js 中的单行中使用。为什么?

c - 防止 gcc 内联所有函数

将一段字符数组更改为C中的整数数组

c - 记住最后执行的命令的简单历史记录

c++ - 指向成员转换的指​​针

JavaScript 名称与 ID