简体   繁体   中英

Why does the sign bit not affect the Integer.MAX_VALUE but affects the MIN Value?

System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);

Prints:

2147483647
-2147483648

Why is the max value 2^31 -1 (the sign bit is 0 and does not add to the value of the number) and yet the min value is just -2^31 (the sign bit is 1 and does add to the value then??).

Think about it this way: you have as many binary patterns with the sign bit set to 1 as the number of binary patterns with the sign bit set to 0 . However, you also need to represent zero, which is neither positive nor negative. Since zero is represented as a pattern of all zeros, it deducts from the set of positive numbers representable with the given number of bits, so the count of representable negative numbers is going to be greater by one.

The sign bit does not add to the value. You can represent 2^32 different values with 32-bits. Hovewer, one of those values is 0; so there are 2^31 negative values, 2^31 - 1 positive values and 0, which all add up to 2^32 different values. Since the sign bit of 0 is also 0, it is only natural that the number of positive values are one less then the number of negative values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM