1

Is there a formula to calculate what the overflow of a Java int would be?

Example: if I add 1 to Integer.MAX_VALUE; the answer is not 2147483648, but rather -2147483648.

Question: if I wanted to calculate what Java would print for a value larger than 2^32, is there an easy mathematical expression (theoretical, not in code)?

8
  • 1
    Use a long instead. Commented May 5, 2014 at 20:21
  • 1
    Integer.MAX_VALUE + 1 == Integer.MIN_VALUE. Commented May 5, 2014 at 20:22
  • @BoristheSpider how would you translate (INTEGER.MAX_VALUE * 10 - INTEGER.MIN_VALUE) * randomNumber;? Commented May 5, 2014 at 20:23
  • 3
    @Luiggi honestly; I would plug it into a main method and print the output. Commented May 5, 2014 at 20:24
  • But is there an expression (not in code) that is just algebraic based. Example: +/-(2147483648 - |int|)? Commented May 5, 2014 at 20:26

3 Answers 3

5

((x + 231) mod 232) - 231

Is this what you're looking for? That should be the result of any mathematical operation on a machine that uses 32-bit signed 2's complement integers. That is, if the mathematical value of an operation returns x, the above formula gives the integer that would actually be stored (if the operation doesn't fault, and it's not a "saturating" operation).

Note that I'm using "mod" with a mathematical definition, not the way the % operator works in Java or C. That is, A mod B, where A and B are integers and B > 0, always returns an integer in the range 0 .. B-1, e.g. (-1) mod 5 = 4. More specifically, A mod B = A - B*floor(A/B).

Sign up to request clarification or add additional context in comments.

2 Comments

Let me see if I have this straight. For nonnegative values of x <= 2^31-1, the addition operation in the formula corresponds to flipping the 31st bit from 0 to 1, the result of the modulo operator is the same value, and then subtracting 2^31 flips the 31st bit back to 0. For the other cases where x>2^31-1 or when x is negative, what is going on with the binary representation of x during this operation?
@NicholasCousar This is intended to be a mathematical formula for what the result of the computer's operation will be. It is not intended to describe what the computer actually does with the binary representation of the number. Trying to think of this formula in terms of a binary representation will probably just create confusion.
4

In java an int is 32 bits but it is also signed, which means that the first bit is the "negative" sign. 1 means negative and 0 means positive. Because of this, the largest number is 2147483647 (0111 1111 1111 1111 1111 1111 1111 1111). If you add 1 it makes it 1000 0000 0000 0000 0000 0000 0000 0000 which translates to -2147483648. For any values larger than that you would need to use a long

Comments

0

I believe this would work:

 int expected = ((val + Integer.MAX_VALUE) % Integer.MAX_VALUE) - Integer.MAX_VALUE;

1 Comment

On the right track, but not correct. MAX_VALUE is 2^31 - 1 and you definitely don't want to use that with your modulo operator.

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.