wood burning stoves 2.0*
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
Author

shift operators

raghuram gannavarapu
Greenhorn

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;
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....
thanx in advance ..
raghu
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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: http://aspose.com/file-tools
 
subject: shift operators
 
Similar Threads
Shift by a negative value
elementary questions which keep on confusing me
Help Me Out With Shift Operators...
shift operators
shifting operators... please help !