Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# Qn on abs () method

cs singh
Ranch Hand

Joined: Dec 28, 2005
Posts: 36

why the ans is c here.?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The reason is because of the asymetry in the number of negative and positive integers that can be represented.

There are 2^31 negative whole numbers representable by an int, and 2^31-1 positive whole numbers representable by an int.

So there is one more negative whole number representable than there are positive whole numbers representable.

So Integer.MIN_VALUE has no positive counterpart.

Another way to look at it is the bit representation.

This is the bit representation of Integer.MIN_VALUE

10000000000000000000000000000000

In order to get the negative of a number we apply the complement operator to get the 1's complement and add 1 to get the 2's complement.

When we apply the complement operator we get

01111111111111111111111111111111

When we add 1 we get

10000000000000000000000000000000
[ March 14, 2006: Message edited by: Keith Lynn ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8752

4
And for those of you studying for the 1.5 exam, don't panic! This topic isn't on the exam.

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
cs singh
Ranch Hand

Joined: Dec 28, 2005
Posts: 36
Thanks a lot Keith for your valuable time...
got it now.

I agree. Here's the link: http://aspose.com/file-tools

subject: Qn on abs () method