gpt4 book ai didi

java - 为什么 Java 溢出返回 0 作为值

转载 作者:行者123 更新时间:2023-11-29 04:06:25 25 4
gpt4 key购买 nike

我有以下代码片段

public class DN1{
public static void main(String argv[]){
int a = 869;
int b = 85;
for(int i = 0; i < a; i++){
b += b;
}
System.out.println(b);
}
}

此代码在 i = 24 处明显溢出

我想知道为什么在几个循环之后 b 设置为 0。这种溢出行为是什么?它在 Java 中究竟是如何工作的?

我希望输出在 -2^31 到 +2^31 之间可变,但事实并非如此,这是为什么?

最佳答案

更准确地说,输出在您指定的范围内。 0 符合 Integer.MIN_VALUEInteger.MAX_VALUE 之间的条件。

稍微修改您的代码,您可以查看发生了什么并打印实际的二进制文件

int a = 869;
int b = 85;
for(int i = 0; i < a; i++){
System.out.printf(
"Iteration %d: b = %d; b+b = %d; bin(b) = %s; bin(b+b) = %s%n",
i, b, (b+b), Integer.toBinaryString(b), Integer.toBinaryString(b+b));
b += b;
}
System.out.println(b);

给出以下输出

Iteration 0: b = 85; b+b = 170; bin(b) = 1010101; bin(b+b) = 10101010
Iteration 1: b = 170; b+b = 340; bin(b) = 10101010; bin(b+b) = 101010100
Iteration 2: b = 340; b+b = 680; bin(b) = 101010100; bin(b+b) = 1010101000
Iteration 3: b = 680; b+b = 1360; bin(b) = 1010101000; bin(b+b) = 10101010000
Iteration 4: b = 1360; b+b = 2720; bin(b) = 10101010000; bin(b+b) = 101010100000
Iteration 5: b = 2720; b+b = 5440; bin(b) = 101010100000; bin(b+b) = 1010101000000
Iteration 6: b = 5440; b+b = 10880; bin(b) = 1010101000000; bin(b+b) = 10101010000000
Iteration 7: b = 10880; b+b = 21760; bin(b) = 10101010000000; bin(b+b) = 101010100000000
Iteration 8: b = 21760; b+b = 43520; bin(b) = 101010100000000; bin(b+b) = 1010101000000000
Iteration 9: b = 43520; b+b = 87040; bin(b) = 1010101000000000; bin(b+b) = 10101010000000000
Iteration 10: b = 87040; b+b = 174080; bin(b) = 10101010000000000; bin(b+b) = 101010100000000000
Iteration 11: b = 174080; b+b = 348160; bin(b) = 101010100000000000; bin(b+b) = 1010101000000000000
Iteration 12: b = 348160; b+b = 696320; bin(b) = 1010101000000000000; bin(b+b) = 10101010000000000000
Iteration 13: b = 696320; b+b = 1392640; bin(b) = 10101010000000000000; bin(b+b) = 101010100000000000000
Iteration 14: b = 1392640; b+b = 2785280; bin(b) = 101010100000000000000; bin(b+b) = 1010101000000000000000
Iteration 15: b = 2785280; b+b = 5570560; bin(b) = 1010101000000000000000; bin(b+b) = 10101010000000000000000
Iteration 16: b = 5570560; b+b = 11141120; bin(b) = 10101010000000000000000; bin(b+b) = 101010100000000000000000
Iteration 17: b = 11141120; b+b = 22282240; bin(b) = 101010100000000000000000; bin(b+b) = 1010101000000000000000000
Iteration 18: b = 22282240; b+b = 44564480; bin(b) = 1010101000000000000000000; bin(b+b) = 10101010000000000000000000
Iteration 19: b = 44564480; b+b = 89128960; bin(b) = 10101010000000000000000000; bin(b+b) = 101010100000000000000000000
Iteration 20: b = 89128960; b+b = 178257920; bin(b) = 101010100000000000000000000; bin(b+b) = 1010101000000000000000000000
Iteration 21: b = 178257920; b+b = 356515840; bin(b) = 1010101000000000000000000000; bin(b+b) = 10101010000000000000000000000
Iteration 22: b = 356515840; b+b = 713031680; bin(b) = 10101010000000000000000000000; bin(b+b) = 101010100000000000000000000000
Iteration 23: b = 713031680; b+b = 1426063360; bin(b) = 101010100000000000000000000000; bin(b+b) = 1010101000000000000000000000000
Iteration 24: b = 1426063360; b+b = -1442840576; bin(b) = 1010101000000000000000000000000; bin(b+b) = 10101010000000000000000000000000
Iteration 25: b = -1442840576; b+b = 1409286144; bin(b) = 10101010000000000000000000000000; bin(b+b) = 1010100000000000000000000000000
Iteration 26: b = 1409286144; b+b = -1476395008; bin(b) = 1010100000000000000000000000000; bin(b+b) = 10101000000000000000000000000000
Iteration 27: b = -1476395008; b+b = 1342177280; bin(b) = 10101000000000000000000000000000; bin(b+b) = 1010000000000000000000000000000
Iteration 28: b = 1342177280; b+b = -1610612736; bin(b) = 1010000000000000000000000000000; bin(b+b) = 10100000000000000000000000000000
Iteration 29: b = -1610612736; b+b = 1073741824; bin(b) = 10100000000000000000000000000000; bin(b+b) = 1000000000000000000000000000000
Iteration 30: b = 1073741824; b+b = -2147483648; bin(b) = 1000000000000000000000000000000; bin(b+b) = 10000000000000000000000000000000
Iteration 31: b = -2147483648; b+b = 0; bin(b) = 10000000000000000000000000000000; bin(b+b) = 0

您实质上是将位向左移动,逻辑上等同于每次迭代时乘以 2。您最终在第 31 次迭代中得到二进制 10000000000000000000000000000000 并将其向左移动会将 1 推出序列,因此您得到 000000000000000000000000000000000。这发生在第 31 次迭代时,无论 b 的起始值如何(您只有 32 位可以使用,位置 0-31)。

补充:b += b的其他写法:

b <<= 1
b *= 2

关于java - 为什么 Java 溢出返回 0 作为值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58365636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com