特别是我这样做
Word32 x = 18653184;
Word32 y;
Word16 shift = 269;
y = x >> shift;
我希望这个逻辑移位的结果为0,但我得到2277.C如何定义这种类型的操作?
答案 0 :(得分:5)
是的,根据6.5.7第3节
,它是未定义的行为...如果右操作数的值为负或大于或等于提升的左操作数的宽度,则行为未定义。
答案 1 :(得分:3)
ISO c99 : 6.5.7 Bitwise shift operators
3
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.
5
The result 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 nonnegative value, the value of the result is the integral
part of the quotient of E1 / 2E2. If E1 has a signed type and a negative value, the
resulting value is implementation-defined
你可以看到c-standard澄清了一切。