c - C 左移运算中的模行为

标签 c binary bit-manipulation

按位移位运算符的 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/

相关文章:

c - 填充 C 结构体

python - 将字符串转换为 64 位整数映射字符到自定义两位值映射

c - C 中 int 文字之间的零

c - 为什么输出看起来像这样?

python - 如何从Python字符串中更改1位?

c - 使用计数器循环数组

c - 您是否在 C 库中定义全局变量?

c++ - MakeFile.am : Mixing c and c++, 新错误

java - 有效地将十六进制转换为二进制

r - 使用大型数据集在 R 中创建二元矩阵