File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "shift operators" Watch "shift operators" New topic
Author

shift operators

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
how do we evaluate -1>>-1. The o/p : -1.Is there any rule
that -ve no >>-1 is -1.
Thanks!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
As I recall, the shift operator ignores the sign. For a 32 bit shift, it masks off the low 5 bits and uses that value, for that reason, a shift of 33 is the same as a shift of one.
Masking the low five bits of -1 gives you 11111 = 31 places shifted.
Shift of long (64 bit) values masks off the low 6 bits.
Bill
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
But can we shift by -1...what is the rule regarding -ve shift of a number...whether +ve or -ve?
Please clear my confusion
mita
Ranch Hand

Joined: Jul 19, 2000
Posts: 50
how do we evaluate -1>>-1. The o/p : -1.Is there any rule
that -ve no >>-1 is -1.
Thanks!
shifting by -1, is equivalent to shifting by 31.(-1 + 32 = 31).
So -1 >> -1 equivalent to -1 >> 31 .The result is -1.
When we do shifting by -ve numbers, for a 32bit we add 32 to the negative number & add 64 in case of 64 bits.
Hope it helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: shift operators
 
Similar Threads
Are there any Parsi guys on JR?
Portal and portlet developement forum
count yourself in javaranch#~
How many rotations...
0 or 1