• 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 operator

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who can tell me why:
int i=1;
i<<=31;<br /> i>>=31;
the running result is i=-1;
int i=1;
i<<=31;<br /> i>>>=31;
the result is i=1;
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jean..
well.. the result has to do with signed right shift(>>) and unsigned right shift operators(>>>)
before we start on an example, let it be clear that a signed right shift fills the higher-order bits with the sign bit of the number (1 or 0) whereas unsigned right shift fills the higher-order bits with 0s regardless of the sign bit..
1 when represented in binary becomes:
Number Binary Representation(Memory)
i 1 00000000000000000000000000000001
i i<<31 10000000000000000000000000000000 <br /> i i>>31 11111111111111111111111111111110
and this is the 2's complement representation of -1, so i comes out to be -1.
In the second case when we apply the unsigned right shift operator..
i i>>>31 00000000000000000000000000000001
the higher-order bits are filled with 0s instead of 1(as already explained) so, i comes out to be 1.
hope this helps..
------------------
Hima
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jean,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i=1;
00000000 00000000 00000000 00000001
i<<=31<br /> 10000000 00000000 00000000 00000000<br /> i>>31 : inserts sign bit 0/1 in shifted bit position
11111111 11111111 11111111 11111111
which is -1 in binary notation
int i=1;
00000000 00000000 00000000 00000001
i<<=31<br /> 10000000 00000000 00000000 00000000<br /> i>>>31 : inserts 0 in shifted bit position
00000000 00000000 00000000 00000001
which is 1 in binary notation
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Hima and Sweekriti,I can understand the shift operator
right now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic