This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Stupid Java Question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Stupid Java Question.

Milan Gupta
Greenhorn

Joined: Aug 03, 2002
Posts: 7
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

Joined: Jan 30, 2000
Posts: 18671
Follow-ups go to the new, improved version here.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Stupid Java Question.
 
Similar Threads
shift question
Reading a String backward
To Cast or Not To Cast
Stupid Question asked better...
Integer and Short