0

I have read here (https://stackoverflow.com/a/27762490/4415632) that when integer overflow occurs, the most significant bits are simply cut off.

However, I have also read here (https://stackoverflow.com/a/27747180/3808877) that when overflow occurs, "the value becomes the minimum value of the type, and start counting up again." Which one is correct, or are both answers correct? If so, can anyone show me why those two interpretations are equivalent to each other?

3 Answers 3

2

Both are correct, it depends on context. One is the result of casting and one is the result of overflow. Those are different operations. For example, if you cast Long.MAX_VALUE to an int that is a cast operation

System.out.println((int) Long.MAX_VALUE); // <-- -1

If you overflow an int by adding one to Integer.MAX_VALUE then

System.out.println(Integer.MAX_VALUE + 1); // <-- Integer.MIN_VALUE
Sign up to request clarification or add additional context in comments.

3 Comments

Out of curiousity, do both of the approaches give the same results ? I was one of the answerers on the second question and was surprised to see that the binary interpretation of the accepted answer gave the same result as the small algorithm I had written
Both approaches give the same result because both approaches are actually the same very same thing.
Actually I don't think this is correct regarding MSB
2

Both interpretations are correct, because they are actually the same.

Let's look at the maths to see why.

Java stores values in byte, short, char, int and long in a format called two's complement. In case of byte, short, int and long it is signed, in case of char it is unsigned. One of the attributes of the two's complement format is that for most operations it does not matter whether the value is interpreted as signed or unsigned as the resulting bit pattern would be the same.

To shorten things, I'll explain it using byte, but the other types work along the same scheme.

A byte has 8 bits. The topmost bit is interpreted as sign bit. So, the bit pattern goes like this:

snnn nnnn

The separation into two groups of 4 bits each is called Nibble and is performed here for pure readability. As a side note, a nibble can be represented by a hexadecimal digit.

So there are 8 bits in a byte, and each bits could be 0 or 1. This leaves us with 2^8 = 256 different values that could be stored in a byte.

Here are some sample values:

0000 0000 -> 0
0000 0001 -> 1
0000 0010 -> 2
0100 0000 -> 64
0111 1111 -> 127
1000 0000 -> -128
1111 1110 -> -2
1111 1111 -> -1

The 2's complement value of signed numbers which are negative, i.e. the sign bit is set, is created by taking the positive value of the 8 bits and subtracting the range, i.e. in case of a byte by subtracting 256.

Now let's see what happens if you take -1 and add 1.

     1111 1111 -1 / 255
+    0000 0001  1
--------------
= 1  0000 0000 -0 / 256 intermediate result
=    0000 0000  0 / 256 result after dropping excess leading bits

There is an overflow. The result would need 9 bits now, but the byte only has 8 bits, so the most significant bit is lost.

Let's look at another example, -1 plus -1.

     1111 1111 -1 / 255
+    1111 1111 -1 / 255
--------------
= 1  1111 1110 -2 / 510 intermediate result
=    1111 1110 -2 / 254 result after dropping excess leading bits

Or this, 127 plus 5.

     0111 1111 127
+    0000 0101 5
--------------
=    1000 0100 132 / -124

As we can see, the leading bits are dropped and this actually is what leads to the effect that causes it to overflow by "starting to count from the minimum value again".

Comments

-2

I add another option: a processor trap. Some processors will generate a trap on integer overflows. When available, this feature usually can be enabled in user mode by setting a bit in the processor status register.

1 Comment

Sure, and now show us how to do that in a way that is compatible with the JVM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.