Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??).

like image 213
PragmaticProgrammer Avatar asked Dec 06 '25 13:12

PragmaticProgrammer


1 Answers

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.

like image 176
Sergey Kalinichenko Avatar answered Dec 09 '25 02:12

Sergey Kalinichenko