File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How is -Integer.MIN_VALUE Equal to Integer.MIN_VALUE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How is -Integer.MIN_VALUE Equal to Integer.MIN_VALUE" Watch "How is -Integer.MIN_VALUE Equal to Integer.MIN_VALUE" New topic
Author

How is -Integer.MIN_VALUE Equal to Integer.MIN_VALUE

Avinash Haridasu
Ranch Hand

Joined: Jul 12, 2011
Posts: 31
I observed an interesting fact

System.out.println(-Integer.MIN_VALUE == Integer.MIN_VALUE);//output: true

System.out.println(-Integer.MIN_VALUE); // output : -2147483648

Why is that a negative int remains negative when negated???

----------------------------------
Regards

Avinash
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Write out its bit pattern. HInt: you can get it from Integer.toBinaryString(Integer.Min_VALUE). It might be 1 and thirty-one 0s. Or it might not be.
Work out its two's complement when negated: Hint: Write out the bit pattern for 2 to the 32nd. That is 1 and thirty-two 0s. Subtract the binary pattern for -2147483648 from that. And what do you get . . . Tadaaa!

Google for arithmetic overflow and two's complement binary integers, and you will get a different explanation of the same thing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Negating an int and getting the same value works for the value you have shown, and one other value. I shall let you guess which other value.
Avinash Haridasu
Ranch Hand

Joined: Jul 12, 2011
Posts: 31
Can any one tell me
how do we decide whether a number is negative or positive just by looking at the binary representation

0 0 0 0 0 0 0 1 = 1
1 1 1 1 1 1 1 1 = −1
and

System.out.println( 32 >> 37); // output: 1
System.out.println( 32 >> 5); // output: 1

How is this possible???
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Avinash Haridasu wrote:Can any one tell me
how do we decide whether a number is negative or positive just by looking at the binary representation

0 0 0 0 0 0 0 1 = 1
1 1 1 1 1 1 1 1 = −1

.
In binary number system we can use only 2 things and that is 0 and 1 and we have to use 0 and 1 for representing the negative numbers also.There is something known as MSB(most significant bit) and LSB(least significant bit). Now the left-most-bit is your MSB and right-most-bit is LSB. Example 01010101 Here the one in red color is MSB and the one in green color is LSB..
.
So you must have understood what is MSB and LSB. Now if the MSB is 0 then the number is positive and if the MSB is 1 then the number is negative. So the conclusion is MSB represents the sign of the number and other numbers(i would say bits ) are the actual value. So
Since MSB is 0 therefore number is positive and waht about the value 0000001 is equal to 1 and therefor ans is . Now Now MSB is 1 therefore number is negative and Java uses 2's complement for negative numbers and the 2's complement of 11111111 is 00000001 and therefore the ans is -1 (Please find out how to find 2's complement if you are not aware).
.
As for your second question about >> i am not sure.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Your description of the values of LSB and MSB is correct, but you ought to remember that only applies to two's complement numbers. The char for example is unsigned and behaves differently.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Look in the Java Language Specification and you find that the number of bits shifted is calculated % i where i is the number of bits in the datatype. So for everything except a long, shift 37 and shift 5 have the same effect.
i >> 5 is equivalent to i / 32

Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Campbell Ritchie wrote:but you ought to remember that only applies to two's complement numbers. The char for example is unsigned and behaves differently.

Behaves differently ? Can you give an example so that i can understand it better?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

char is like an unsigned 16-bit number. You can't store negative values in a char. So a char with the bit pattern 1111 1111 1111 1111 has the value 65536, not -32768. In other words, for a char, two's complement is not used.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3378
    
    9
Did you mean -1?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Actually it's not quite i % 32, but more like i & 31, which behaves similarly to i % 32 but gives a positive result from a negative left operand.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Stephan van Hulst wrote:Did you mean -1?
No, Jesper actually means 65535, assuming that is the bit pattern for a char. That is equivalent to the Unicode character for ffff, and you can find what that is here.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3378
    
    9
Sorry, I was referring to Jesper's:
not -32768

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Yes, I got confused about that. I agree that's "not -1".
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Ok, again...:

char is like an unsigned 16-bit number. You can't store negative values in a char. So a char with the bit pattern 1111 1111 1111 1111 has the value 65535, not -1. In other words, for a char, two's complement is not used.
Avinash Haridasu
Ranch Hand

Joined: Jul 12, 2011
Posts: 31
Campbell Ritchie wrote:Look in the Java Language Specification and you find that the number of bits shifted is calculated % i where i is the number of bits in the datatype. So for everything except a long, shift 37 and shift 5 have the same effect.
i >> 5 is equivalent to i / 32



As per Specification:

If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.

But why it is "only the five lowest-order bits" ???

Can any one please explain ??

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3378
    
    9
Because you don't need any more than that. With 5 bits you can create the values 0-31, enough to perform any possible shift distance on an int.
Avinash Haridasu
Ranch Hand

Joined: Jul 12, 2011
Posts: 31
Need small clarification on shifting:

32 In Binary: 0000 0000 - 0000 0000 - 0000 0000 - 0010 0000

32>>1:
0000 0000 - 0000 0000 - 0000 0000 - 0001 0000

32>>2:
0000 0000 - 0000 0000 - 0000 0000 - 0000 1000

32>>1:
0000 0000 - 0000 0000 - 0000 0000 - 0000 0001

32>>6:
0000 0000 - 0000 0000 - 0000 0000 - 0000 0000

When 32>>6 is itself 0 - Then how 32>>33 is 16, 32>>38 is 0 ???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
The Java Language Specification link I gave you yesterday explains it all. There are 32 bits to an int, so you cannot shift an int more than 31 bits. You can fit 31 into 5 bits. [A long can be shifted up to 63 bits, and you can fit 63 into 6 bits.] If you shifted it 32 bits, you would come back to where you started, so i >> 32 is the same as i, which is the same as i >> 0. So you can shift an int by |n % 32| bits, where || is the absolute value operator. Now the absolute value of n % 32 is the same (in binary arithmetic) as n & 32 - 1. And as we all know, 32 - 1 = 0x1f. If you write the bit pattern for 0x1f you find it has 5 1s in. So you get 5 bits masked.

32 bits in an int.
32 = 2 to the 5th.
So you shift an int by the rightmost (least significant) five bits (six for a long).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How is -Integer.MIN_VALUE Equal to Integer.MIN_VALUE
 
Similar Threads
Integer operation??
Why Math.abs() is giving negative output??????
shift operators
>>> operator
Math.abs(Integer.MIN_VALUE)) not returning abs val