Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!

# shift operators

raghuram gannavarapu
Greenhorn
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;
System.out.println(s<<32);
}
}
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....
raghu

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
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.