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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift Operators" Watch "Shift Operators" New topic
Author

Shift Operators

Shivani Prajapati
Greenhorn

Joined: Aug 23, 2006
Posts: 9
I couldn�t understand the operators >> and >>>. Can anyone help in it?

Thanks.
Nikhil Bansal
Ranch Hand

Joined: Jan 24, 2005
Posts: 60
Hi Shivani,

Right Shift Operaor >> is used to shift bits to the right for a given number.This operator will hold the sign of the number.

For example, if you have

int x = 0x80000000
x = x >> 4;

Here 0x80000000 equals -2147483648 in decimal and
1000 0000 0000 0000 0000 0000 0000 0000 in binary.

Since the most significant bit is 1 which represents the sign bit, so the right shift operator will fill four 1's from the left.

hence the number will be

1111 1000 0000 0000 0000 0000 0000 0000 or -134217728 in decimal.


>>> is the unsigned right shift operator which will not preserve the sign or the sign of the resultant number will always be positive.

Hence x >>> 4 will give you 0000 1000 0000 0000 0000 0000 0000 0000 or 134217728 in decimal.

Hope it helps.

Nikhil Bansal


ban$al
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
How do the '>>', '>>>', and '<<' operators work?
 
Don't get me started about those stupid light bulbs.
 
subject: Shift Operators
 
Similar Threads
operators
Unary Operator ()
Increment and Decrement Operators
Doubt on != and == when boxing and unboxing is used
Use of shift operators in Java