Tuesday, January 4, 2011

Integer.MIN_VALUE is its own negative

Integer.MIN_VALUE == -(Integer.MIN_VALUE)

This is a very simple fact.

This happens on every language that uses two's complement to represent the numbers.
This issue is reflected in that the representation of the numbers is asymmetric (more negative values than positive values) and the zero has only one representation (which is also the negation of itself). Let's take for example 8 bits numbers (Java Integers are 32 bits but it's the same issue)

Zero in 8 bits:

00000000

two's complement of zero:

11111111 + 1 = 1(discarded)00000000


minimum negative number represented with 8 bits (Remember the first bit represents the sign)

10000000

two's complement of that number:

011111111 + 1 = 10000000
.......................
Quick Java Facts
Top Java books...