按位移位运算符的 ISO C99 标准对左移有以下规定:
The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1×2E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and non-negative value, and E1×2E2 is representable in the result type, then that is the resulting value; otherwise, the behaviour is undefined.
我不明白在移位的情况下“比结果类型中可表示的最大值减少模一”的含义。
最佳答案
它基本上是在谈论 integer wrapping 。如果数学值大于该类型的最大可表示值,则它会换行,即模算术。
关于c - C 左移运算中的模行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44648992/