Bookmark Topic Watch Topic
  • New Topic

Stupid Java Question.

 
Milan Gupta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Report post to moderator
Okay, I feel like I'm missing something here...
For this code:
int i = 1;
System.out.println("i = " + i );
System.out.println("i as binary = " + new Integer(i).toBinaryString(i) );
i <<= 31;
System.out.println("\n\ni = " + i );
System.out.println("i as binary = " + new Integer(i).toBinaryString(i) );
i >>= 31;
System.out.println("\n\ni = " + i );
System.out.println("i as binary = " + new Integer(i).toBinaryString(i) );
System.out.println("value of 11111111111111111111111111111111 = " + new Integer(i).parseInt("-1111111111111111111111111111111", 2));
System.out.println("value of = 10000000000000000000000000000001 " + new Integer(i).parseInt("-0000000000000000000000000000001", 2));

I don't understand why i after >>=31 operation would print out "11111111111111111111111111111111" as binary representation but prints out -1 as in int.
I thought "10000000000000000000000000000001" would be -1 and "11111111111111111111111111111111" would be -2147483647 as the code prints out with parseInt call.
Milan
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
Follow-ups go to the new, improved version here.
 
    Bookmark Topic Watch Topic
  • New Topic