为什么 Integer.MIN_VALUE * 2
等于 0
?
Integer.MAX_VALUE * 2
等于-2
?
让我更好地解释一下:
我知道它会溢出,但为什么会得到这些特定的结果?
最佳答案
Integer.MIN_VALUE= -2147483648
.看一下 2 * -2147483648
的位计算
Integer.MIN_VALUE*2 = Integer.MIN_VALUE+Integer.MIN_VALUE
-2147483648= 10000000 00000000 00000000 00000000 ->32 bit
+10000000 00000000 00000000 00000000
_________________________________________________________________
2* -2147483648= 1 00000000 00000000 00000000 00000000 Result is 0
|
This bit will be omitted due to limitation of 32 bit
Integer.MAX_VALUE=2147483647
,看看2 * 2147483647
的位计算
Integer.MAX_VALUE*2 = Integer.MAX_VALUE+Integer.MAX_VALUE
2147483647= 01111111 11111111 11111111 11111111 ->32 bit
+01111111 11111111 11111111 11111111
_________________________________________________________________
2* 2147483647= 11111111 11111111 11111111 11111110 Result is -2
关于java - 整数最小/最大值特殊溢出行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20282420/