package cn.littleprince.integer;
/**
* Integer 最大值 + 1
*
* @author 349807102
*/
public class IntegerMaxValuePlusOne {
/**
* Java 语言中 int 类型的数据占 4 个字节
*
* @param args 参数
*/
public static void main(String[] args) {
// Integer.MAX_VALUE = 214748364
// 内存中使用补码表示:符号位 + 补码
// 正数的补码是本身
// 0111 1111 1111 1111 1111 1111 1111 1111
int maxValue = Integer.MAX_VALUE;
System.out.println(maxValue);
// 原码 1000 0000 0000 0000 0000 0000 0000 0000
// 反码 1111 1111 1111 1111 1111 1111 1111 1111
// 补码 +1
// 1000 0000 0000 0000 0000 0000 0000 0000
int minValue = Integer.MIN_VALUE;
System.out.println(minValue);
// 原码 1000 0000 0000 0000 0000 0000 0000 0000
// 反码 1111 1111 1111 1111 1111 1111 1111 1111
// 补码 +1
// 1000 0000 0000 0000 0000 0000 0000 0000
// 1000 0000 0000 0000 0000 0000 0000 0000
int res = maxValue + 1;
System.out.println(res);
System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE);
}
}
输出:
2147483647
-2147483648
-2147483648
true