Wednesday, 6 April 2011

Integer Overflow and Underslow in Java...


You could imagine that when you have only 2 places you are counting (so adding 1 each time)
 00
 01
 10
 11 100
But the last one gets cut down to "00" again. So there is your "overflow". You're back at 00. Now depending on what the bits mean, this can mean several things, but most of the time this means you are going from the highest value to the lowest. (11 to 00)

So:
System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE);
System.out.println(Integer.MIN_VALUE - 1 == Integer.MAX_VALUE);
prints true twice.

No comments:

Post a Comment