wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shift Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift Operators" Watch "Shift Operators" New topic
Author

Shift Operators

Arsho, Ayan
Ranch Hand

Joined: Nov 14, 2001
Posts: 60
<code>
Hello All :
Why does this piece of code output '-1' ?
int i = 1;
i <<= 31;<br /> i >>= 31;
i >>= 1;
S.O.P (i) is -1
Is it because when the shift occurs first time '<<=' the MSB remains 1???
</code>
[This message has been edited by Arsho, Ayan (edited December 07, 2001).]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
int i=1; // i = 00000000 00000000 00000000 00000001
i <<= 31; // i = 10000000 00000000 00000000 00000000<br /> i >>= 31; // i = 11111111 11111111 11111111 11111111 because this is a signed right-shift
i >>= 1; // i = 11111111 11111111 11111111 11111111
And now i is 0xFFFFFFFF which is -1.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shift Operators
 
Similar Threads
a question from mock exam
>>> gives negative ans
shift ques.
shift question
Bit Shifting question