直接上代码
public static void main(String[] args) {
int low = 1;
int high = Integer.MAX_VALUE;
int result1 = (low + high) / 2;//溢出
int result2 = low + (high - low) / 2;//不溢出
int result3 = low + ((high - low) >> 1);//不溢出
System.out.println("result1=" + result1);
System.out.println("result2=" + result2);
System.out.println("result3=" + result3);
}
结果
result1=-1073741824
result2=1073741824
result3=1073741824