位移运算符似乎换行?

bitwise shift operator seems to wrap?

这是 Windows7 32 位,Visual Studio 2017,在 C 文件中。

int i = 65536;

不出所料,

i >> 0 = 65536
i >> 1 = 32768
  :
  :
i >> 16 = 1
i >> 17 to 31 = 0.

i >> 32 又神奇地变成了 65536。这怎么合法?

ISO/IEC 9899:TC2 表示以下内容,我假设 C++ 规范相同?

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 / 2^E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.

6.5.7p3

... 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.

使用 clang、gcc 和 icc,如果您尝试移动一个大于或等于移动值宽度的常量,您将 get a warning,并且您甚至不需要任何额外的command-line 旗帜。