Why is absolute of Integer.MIN_VALUE equivalent to Integer.MIN_VALUE

前端 未结 6 1279
感动是毒
感动是毒 2020-12-31 12:12

In java when I say Integer i = Math.abs(Integer.MIN_VALUE). I get the same value as the answer,which means i contains Integer.MIN_VALUE

相关标签:
6条回答
  • 2020-12-31 12:40

    Math.abs(int) docs says If the argument is negative, the negation of the argument is returned. JLS 15.15.4. Unary Minus Operator says For all integer values x, -x equals (~x)+1.

    -Integer.MIN_VALUE = ~Integer.MIN_VALUE + 1 = ~0x80000000 + 1 = 0x7FFFFFFF + 1 = 0x80000000 = Integer.MIN_VALUE

    0 讨论(0)
  • 2020-12-31 12:45

    You might expect that absolute value of Integer.MIN_VALUE would be Integer.MAX_VALUE + 1, but this value is out of primitive int size. And Integer.MAX_VALUE + 1 is Integer.MIN_VALUE again. That's all.

    0 讨论(0)
  • 2020-12-31 12:48

    There is an inherent asymmetry that is the root cause of this effect. The number of 32-bit bit patterns is even. One of those patterns is used for zero. That leaves an odd number of non-zero values. The number of positive values and the number of negative values cannot be equal because their sum is odd.

    In the 2's complement representation used for Java integers, the number of negative numbers is one greater than the number of positive numbers. Each negative number other than Integer.MIN_VALUE corresponds to a positive number that is both its negation and its absolute value. Integer.MIN_VALUE is left over, with no corresponding positive int. Math.abs and negation map it to itself.

    0 讨论(0)
  • 2020-12-31 12:52

    Read this in Effective Java by Joshua Bloch.

    I found the answer for this question and here is the explanation: Computers work with binary arithmentic, the logic of Math.abs in java or the absolute function in any language is like below:

    if(num >= 0)
        return num;
    else
        return (2's complement of the num);
    

    Note : How to find 2's complement

    For a given number, we find it's 1's complement first and then add 1 to it. For e.g. Consider our number to be 10101 1's complement= 01010 2's complement= 01011 (added 1 to the 1`s complement)

    Now, for the sake of making it easy and clear, let us say that our Integer(signed) size is 3 bit, then here is the possible list of numbers which can be produced using the four bits:

    000 --> 0 (0)
    001 --> 1 (1)
    010 --> 2 (2)
    011 --> 3 (3) 
    100 --> 4 (-4)
    101 --> 5 (-3)
    110 --> 6 (-2)
    111 --> 7 (-1)
    

    Now that this is signed, it means half of the numbers are negative and the other half are positive(The negative numbers are the ones with the first bit 1). Let us start from 000 and try to find its negative number, it would be the two's complement of 000.

    2's complement of `000` = 1 + `111` = `000`
    2's complement of `001` = 1 + `110` = `111`
    2's complement of `010` = 1 + `101` = `110`
    2's complement of `011` = 1 + `100` = `101`
    2's complement of `100` = 1 + `011` = `100`
    2's complement of `101` = 1 + `010` = `011`
    2's complement of `110` = 1 + `001` = `010`  
    2's complement of `111` = 1 + `000` = `001`
    

    From the above demonstration, we find that 2's complement of 111(-1) is 001(1), similarly 2's complement of 110(-2) is 010(2), 2's complement of 101(-3) is 011(3) and 2's complement of 100(-4) is 100(-4) and as we can see that -4 is the smallest negative number possible using 3 bits.

    This is the reason why absolute of Integer.MIN_VALUE is Integer.MIN_VALUE.

    0 讨论(0)
  • 2020-12-31 12:55

    Why this behavior?

    It is a mathematical consequence of the choice of representation used in all modern computers.

    Here's an informal proof of why it has to be that way.

    1. A signed binary number representation with N bits has 2N possible values; i.e. a set integers with an even number of elements.

    2. Remove zero from the set. The set is now has an odd number of elements.

    3. Now remove all pairs of numbers of the form {n, -n}. Each time we remove a pair of numbers, the set still contains an odd number of elements.

    4. We are now left with a set that contains an odd number of integers for which n is in the set, but -n is not in the set. Since the set size is odd, it cannot be empty; i.e. there is at least one number with this property.

    In the representations specified by Java (and indeed used all other practical languages), integer types have 2N-1 negative values and 2N-1 - 1 values greater than zero. The value that has the strange property is MIN_VALUE. This representation is called two's complement.


    Strictly speaking, an integer representation doesn't have to have this anomaly:

    • It is possible to have two zeros (-0 and +0); e.g. signed magnitude or one's complement representations. (This invalidates step 2 of the proof: there are now 2 zeros to remove.)

    • It is possible to exclude -2N-1; i.e. make it an illegal value. (This invalidates step 1 of the proof: the initial set now has an odd number of values.)

    • It is possible to specify integer arithmetic so that (for example) negating MIN_VALUE raises an exception. For instance Math.abs(Integer.MIN_VALUE) would throw an exception.

    However, all of these things have significant performance implications, especially since modern computer hardware only supports twos-complement arithmetic natively. They also have issues in relation to writing reliable integer codes ...

    0 讨论(0)
  • 2020-12-31 12:56

    This is because of the way two's-complement number systems work. Integer.MIN_VALUE corresponds to 0x80000000. The standard way to negate it is to take its ones' complement (0x7FFFFFFF in this case) and add 1, and in this case it would overflow back to 0x80000000.

    0 讨论(0)
提交回复
热议问题