Search code examples
javainteger-overflowatoi

Number overflow in Java


I am tring to implement C/C++ atoi function in Java, below is the code snippet

    for (int j = 0; j < s.length(); j++) {

        int digit = Character.digit(s.charAt(j), 10);
        if (sum < limit/10) {
            if (neg) return Integer.MIN_VALUE;
            return Integer.MAX_VALUE;
        }

        sum *= 10;
        if (sum < limit + digit) {
            if (neg) return Integer.MIN_VALUE;
            return Integer.MAX_VALUE;
        }
        sum -= digit;
    }

For line "if (sum < limit + digit) {", which is correct, however, if I use "sum - digit < limit", it will get wrong result, e.g. input "-2147483649", wrong result 2147483647, should be -2147483648.

I figured this out, cuz sum - digit might be overflow, so this come to another question:

    int sum = Integer.MAX_VALUE;
    System.out.println(sum < Integer.MAX_VALUE + 1);

Why this prints false? What is the behind logic?


Solution

  • Integer.MAX_VALUE + 1 is equal to Integer.MIN_VALUE, which will be more obvious if you look at them in hexadecimal:

    Integer.MAX_VALUE = 0x7fffffff
                    1 = 0x00000001
                        ---------- +
                        0x80000000
    

    And 0x80000000 is also known as Integer.MIN_VALUE.

    Obviously there's no int lower than Integer.MIN_VALUE.

    Also, attempting to test whether a number has overflowed by seeing if it's bigger than the biggest possible value is fundamentally misguided. It can't be bigger than the biggest possible value, that's what "biggest possible" implies. Also, you can't take a number, look at it, and determine whether it has overflowed or not, because every number can be the result of a non-overflowing computation (and indeed of just writing it down as a constant) and as the result of a computation that overflowed. You need to know how you got that number.