File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "shift operators" Watch "shift operators" New topic

shift operators

Preetha Vasudevan
Ranch Hand

Joined: Sep 23, 2005
Posts: 33
can anyone explain me how exactly the shift operations << ,>> and >>> work!!
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1599
Hi Preetha!
Check out numbering over here.

Then come to shifts,

a << 3

Actually means shift/remove left most 3 bits and add three zeros in the other end.
For ex.
if a = 0000 0101 1101 0011 0000 0000 0000 1101 then
a<<3 would be
0010 1110 1001 1000 0000 00 0001 1010

>>4 means remove right most 4 bits and add four sign bit yes sign-bit in the other end. (So +ve numbers will remain +ve and -ve will remain -ve)

>>> is called zero-filled shift. Same as above but fills zeros instead of sign bits. i.e. result will always be +ve. Hope this helps.

[ October 04, 2005: Message edited by: Akhil Trivedi ]
[ October 04, 2005: Message edited by: Akhil Trivedi ]

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Preetha Vasudevan
Ranch Hand

Joined: Sep 23, 2005
Posts: 33
Thanks Akhil
Bert Bates

Joined: Oct 14, 2002
Posts: 8898
remember everyone - this topic is on 1.4 but not tiger...

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
Hi Akhil,

How about those right operands that are negative numbers. How is this done?

a. 8 << -3 = 0
b. 8 >> -3 = 0
c. 8 >>> -3 = 0

Can you explain what is being done for negative numbers on the right operand? Can we move bit negatively?

"If you fail to plan, you plan to fail."<br /> <br />Chad<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS (next)
Ramya Iyer
Ranch Hand

Joined: Jul 29, 2003
Posts: 66
Please look at

The above thread provides the explanation if right operand is negative.
I agree. Here's the link:
subject: shift operators
It's not a secret anymore!