programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Question from Khalidmughal for >>> operator

Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
Hi All,
I have the following example can some guru explain me why am i getting this answer.
public class Myclass
{
public static void main ( String [ ] args )
{
test ( 1 << 32, "1 << 32" );
test ( 1 << 31, "1 << 31" );
test ( 1 << 30, "1 << 30" );
test ( 1, "1" );
test ( 0, "0" );
test ( -1, "-1" );
}
public static void test ( int i, String exp )
{
if (( i >> 1 ) != ( i >>> 1 ))
System.out.println ( exp );
}
}

a) "1<<32" b) "1<<31" C) "1<<30" d) "1" e) "0" f) "-1"
the answer is b & f.
i would really appreciate if someone can explain this.
Ketu

Author and all-around good cowpoke
Posts: 13078
6
• Number of slices to send:
Optional 'thank-you' note:
One key point is how x << 32 is evaluated.
For int shifts, the JVM masks off the low 5 bits,which means that no shift outside the range 0 to 31 can ever be done.
x << 32 is the same as x << 0 for x an int.
Bill

K2Joshi
Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
Hi Bill,
Thanks for the explaination but can you explain me in detail.
like what about i >> 30. My problem is, i am not understanding how this is evolving.
Ketu

Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by K2Joshi:
Hi All,
I have the following example can some guru explain me why am i getting this answer.
public class Myclass
{
public static void main ( String [ ] args )
{
test ( 1 << 32, "1 << 32" );<br /> test ( 1 << 31, "1 << 31" );<br /> test ( 1 << 30, "1 << 30" );<br /> test ( 1, "1" ); <br /> test ( 0, "0" );<br /> test ( -1, "-1" );<br /> }<br /> public static void test ( int i, String exp )<br /> { <br /> if (( i >> 1 ) != ( i >>> 1 )) <br /> System.out.println ( exp );<br /> } <br /> } <br /> <br /> select all valid answers.<br /> a) "1<<32" b) "1<<31" C) "1<<30" d) "1" e) "0" f) "-1"<br /> the answer is b & f. <br /> i would really appreciate if someone can explain this.<br /> Thanks in advance<br /> Ketu

<br /> Hi ketu,<br /> look at " if (( i >> 1 ) != ( i >>> 1 )) ".When it will be satisfied? only when the "i" is negative.Now search howmany of the the above mentioned test calls result in -ve.<br /> <br /> 1) test ( 1 << 32, "1 << 32" ); <br /> // shifts 0 times.so i is +ve.<br /> i = 0000 0000 0000 0000 0000 0000 0000 0001 <br /> i >> 1 = 0000 0000 0000 0000 0000 0000 0000 0000 <br /> i >>> 1 = 0000 0000 0000 0000 0000 0000 0000 0000 <br /> <br /> 2) test ( 1 << 31, "1 << 31" ); <br /> // i here is -ve.<br /> i = 1000 0000 0000 0000 0000 0000 0000 0000 <br /> i >> 1 = 1100 0000 0000 0000 0000 0000 0000 0000 <br /> i >>> 1= 0100 0000 0000 0000 0000 0000 0000 0000<br /> <br /> 3) test ( 1 << 30, "1 << 30" );<br /> i = 0100 0000 0000 0000 0000 0000 0000 0000 <br /> i >> 1 = 0010 0000 0000 0000 0000 0000 0000 0000 <br /> i >>> 1= 0010 0000 0000 0000 0000 0000 0000 0000<br /> <br /> 4) test ( 1, "1" ); <br /> i = 0000 0000 0000 0000 0000 0000 0000 0001<br /> i >> 1 = 0000 0000 0000 0000 0000 0000 0000 0000 <br /> i >>> 1 = 0000 0000 0000 0000 0000 0000 0000 0000

5) test ( 0, "0" );
// i = 0000 0000 0000 0000 0000 0000 0000 0000

6) test ( -1, "-1" );
// i = 1111 1111 1111 1111 1111 1111 1111 1111

Hence the obvious answers for the question are: b & f( hgere only i becomes -ve and satisfies if condition.

K2Joshi
Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
Thanks a billion jay,
I would have never understand without anyones help, thanks for taking your time.
Ketu