Is right shift undefined behavior if the count is larger than the width of the type?
我刚刚检查了C++标准。似乎以下代码不应是未定义的行为:
1 2 3 | unsigned int val = 0x0FFFFFFF; unsigned int res = val >> 34; // res should be 0 by C++ standard, // but GCC gives warning and res is 67108863 |
根据标准:
The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1
has an unsigned type or if E1 has a signed type and a non-negative
value, the value of the result is the integral part of the quotient of
E1/2^E2. If E1 has a signed type and a negative value, the resulting
value is implementation-de?ned.
号
根据标准,由于34不是负数,所以变量
gcc对代码段给出以下警告,
warning: right shift count >= width of type
号
我还检查了gcc发出的汇编代码。它只调用
这是否意味着GCC不在英特尔平台上实现标准行为?
第1段EDOCX1·0移位算子的C++标准草案(强调矿山):
The type of the result is that of the promoted left operand. The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand.
号
因此,如果unsigned int等于或小于
要准确解释发生了什么:编译器将把
由于0x0fffffff(268435455十进制)除以4=67108863,这就是您看到的结果。
如果你有一个不同的处理器,例如一个PowerPC(我认为),它可能会给你零。