gcc 4.8.4 警告 1u << 63ul
(假设 64 位 long
和 32 位 int
)并计算 0
.是这样吗(在转移之前没有从 1u
提升到 1ul
)?
ISO/IEC 9899:201x, 6.3.1.8(通常的算术转换):“许多期望算术类型操作数的运算符导致转换”; 6.5.7(移位运算符):“对每个操作数执行整数提升...”。
但我并非无法下结论。那些“许多运营商”是哪些?据我了解,“整数提升”不适用于宽度大于 int
的类型(我说得对吗?),但标准并未明确说明隐式类型转换不考虑按位移位的右操作数。
最佳答案
每个操作都单独记录了这一点。例如,n1548 §6.5.5“乘法运算符”¶3
The usual arithmetic conversions are performed on the operands.
该短语在 §6.5.7“移位运算符”中被省略。相反,它说:
The integer promotions are performed on each of the operands. The type of the result is the type of the promoted left operand. …
由于位移运算符部分没有提及“通常的算术转换”,因此不会发生这种转换。
关于C 位移位 : right operand considered for implicit type-conversion?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42787336/