• Post Reply Bookmark Topic Watch Topic
  • New Topic
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

Shift Operators ??????????

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This Q is from Khalid Mugal, Pg no.-72, qno.-3.11
Accorind to me the ans is only (f),but according to book it is both (b) and (f) . Pls Explain.
The Q is :-
What would be printed during execution of the following program?
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);
}
}
Select all valid answers.
(a)"1<<32"
(b)"1<<31"
(c)"1<<30"
(d)"1"
(e)"0"
(f)"-1"
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The operation 1<<31 puts a 1 bit in the high order (sign) bit of an integer, making it a negative number.<br /> When the (i >> 1) operation is performed the sign bit is preserved, creating -1, but when (i >>> 1) is performed, the sign bit is set to zero , creating 1 and -1 != 1 QED.
Bill
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi william,
in the statement
The operation 1<<31 puts a 1 bit in the high order (sign) bit of an integer, making it a negative number.
isn't it wrong to say that << operator puts a 1 bit as it always puts zero in any case
please correct me
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it would be better to say it shifts a 1 bit to the high order (sign) bit. The fill bits that are shifted into the low order bits are indeed zero.
Bill

------------------
author of:
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!
can some one explain me how (int i=1),i<<=31 gives a negative int.
because i<<=31 gives
10000000 00000000.....000
if MSB gives the sign then how to perform i>>=31 after the above operation.i am confused please someone explain me in detail.
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please help!
how shift works on 1<<=31 and changes sign!
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Nachiket,
an int has 32 bits. So 1 is written as
00000000000000000000000000000001
Now, the maximum << or >> or >>> that you can do on an int is 31. If the problem says 1<<33 it means 1<<1 (because 33%32=1).
Hence, when you shift 1 to the left 31 times,
1<<31 gives
10000000000000000000000000000000 which is a negative number.
I hope this explains
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Parimala
but now if i perform 1=>>31 again on 1<<=31 will give me<br /> 00000000 00000000 00000000 00000001.then if i perform<br /> 1>>=1 will give me zero. help me.(its regarding the first question in this topic)
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1<<31 gives<br /> 10000000000000000000000000000000 which is a negative number.<br /> I hope this explains<br /> <br /> IP: Logged<br /> <br /> nachiket deshpande<br /> ranch hand posted April 01, 2001 10:18 AM <br /> --------------------------------------------------------------------------------<br /> thanks Parimala<br /> but now if i perform 1=>>31 again on 1<<=31 will give me<br /> 00000000 00000000 00000000 00000001.then if i perform<br /> 1>>=1 will give me zero. help me.(its regarding the first question in this topic)

Peroforming 1=>>31 on 1<<=31 will make it
11111111 11111111 11111111 11111111 which is still a negative number .
since << is signed right shift operator which will fill the signed bit in the left hand side.
 
reply
    Bookmark Topic Watch Topic
  • New Topic