Bitshift 在不应该的时候导致溢出

Bitshift Causing Overflow When It Shouldn't

当我对最大正 2 的补码进行位移时,不应该将它位移 31 位来生成有效的 0,因为它以 0111 1111 等开头。

我试过减少班次,但我假设这只是计算机读取不正确。

int main(void) 
{
  int x = 0x7FFFFFFF;
  int nx = ~x;
  int ob = nx >> 31;
  int ans = ob & nx;

  printf("%d",ans);
}

我希望 ob 为 0,但结果是二进制补码的最小值。我正在使用它来创建爆炸而不实际使用 !.

如果您移动最大正二进制补码,它最终为零。

但是你没有改变那个数字:

int x = 0x7FFFFFFF;
int nx = ~x;          // 0x80000000 (assuming 32-bit int).

您正在对最大(量级) 数字进行位移。

并且,根据标准文档C11 6.5.7 Bitwise shift operators /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 / 2^E2 . If E1 has a signed type and a negative value, the resulting value is implementation-defined.

您的实现似乎保留了符号位,这就是您最终得到非零负值的原因。


顺便说一句,如果你想要一个 ! 运算符,你可以使用:

output = (input == 0);

参见上述标准,6.5.3.3 Unary arithmetic operators /5(再次强调),其中明确指出了等价性:

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !E is equivalent to (0==E).