Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Integer.Min_value

amit taneja
Ranch Hand
Posts: 817
hii
in one of the dan explaination to question...there is written that

If the argument is a negative integral value, then the returned value is the two's complement of the argument. The magnitude of Integer.MIN_VALUE is one greater than the magnitude of Integer.MAX_VALUE; therefore, the absolute value of Integer.MIN_VALUE exceeds the range of Integer.MAX_VALUE. Due to the limited range of type int, the two's complement of Integer.MIN_VALUE is Integer.MIN_VALUE. For that reason, the Math.abs method returns Integer.MIN_VALUE when the argument is Integer.MIN_VALUE. The negation of Short.MIN_VALUE is well within the range of type int; so Short.MIN_VALUE is never returned by the Math.abs method.

i m not able to understand fully what does that means...
if anybody pls explain me with eg. would be gr8 help to me...

marc weber
Sheriff
Posts: 11343
The range of an int is -2^31 to (2^31)-1, which is -2147483648 to 2147483647. Note that the magnitude of the negative bound is one greater than that of the positive bound.

So the absolute value of Integer.MIN_VALUE is actually greater than Integer.MAX_VALUE. In particular, we might expect the absolute value to be +2147483648, but this is one greater than what can be expressed as a positive int, so this overflows to Integer.MIN_VALUE.

In terms of twos complement, flipping the bits of Integer.MIN_VALUE results in Integer.MAX_VALUE, then adding 1 causes the overflow. Note the output from the code below...

This is not a concern when a short is passed to the Math.abs(int) method, because the short is widened to type int, which can easily accomodate Short.MAX_VALUE + 1.
[ May 12, 2005: Message edited by: marc weber ]