• Post Reply Bookmark Topic Watch Topic
  • New Topic

shift operators

 
raghuram gannavarapu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!