位移位和整数提升?
通常,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 是否已损坏,或者标准是否对位移位的类型提升规则做出了一些例外?
Normally, C requires that a binary operator's operands are promoted to the type of the higher-ranking operand. This can be exploited to avoid filling code with verbose casts, for example:
if (x-48U<10) ...
y = x+0ULL << 40;
etc.
However, I've found that, at least with gcc, this behavior does not work for bitshifts. I.e.
int x = 1;
unsigned long long y = x << 32ULL;
I would expect the type of the right-hand operand to cause the left-hand operand to be promoted to unsigned long long
so that the shift succeeds. But instead, gcc prints a warning:
warning: left shift count >= width of type
Is gcc broken, or does the standard make some exception to the type promotion rules for bitshifts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所谓的通常的算术转换适用于许多二元运算符,但不是全部。例如,它们不适用于位移运算符、&&、||、逗号运算符和赋值运算符。这是移位运算符的规则:
The so-called usual arithmetic conversions apply to many binary operators, but not all of them. For example they do not apply to the bit shift operators, &&, ||, comma operator, and assignment operators. This is the rule for the bit shift operators:
真正的麻烦在于,提升仅适用于您的平台定义为
int
的任何内容。正如其他一些答案所述,位移运算符会将左操作数提升为 int。但是,这里int
被定义为 32 位值。整数转换不会提升为long long
(64 位)。The trouble really is that promotion only works up to whatever your platform defines as an
int
. As some other answers have stated, the bit-shift operator will promote the left operand to an int. However, here anint
is defined as a 32-bit value. The integer conversion will not promote to along long
(64-bit).