File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java 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 » Java » Beginning Java
Bookmark "shift operators" Watch "shift operators" New topic

shift operators

raghuram gannavarapu

Joined: Dec 19, 2003
Posts: 3
how can we get the same value when we left shift a number (suppose 10)32 times?
public class abc
public static void main(String [] args)
int s=10;
and if we shift it 31 times it is zero. how can it be?
i am very much confused ...can anyone pls explain it in detail....
thanx in advance ..
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Section 15.19 of the Java Language Spec states, in talking about shift operators:

If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (�15.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.

So a shift by 32 bits is interpreted as a shift by 0 bits.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: shift operators
It's not a secret anymore!