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/3482262/

相关文章:

c - 为什么我在编译时出现无法识别文件格式的错误?

javascript - 当多个元素共享相同的 id 时,标准错误行为是什么?

c - 从 main() 访问单独文件中的静态函数

c++ - 如何拒绝 MSVC 自己包含 header

c++ - "shld"指令产生奇怪的值

python - 从 python 调用静态链接的 C 库在 Windows 上永远挂起,但在 OSX 或 Linux 上则不然

java - 用 Java 位置编写的 Linux 守护程序

xml - HTML 是否应该尽可能像 XML 一样格式化?

c++ - Eclipse CDT C/C++ : Include a header file from another project

c - 如何在C中连接数组中的十六进制数据