• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

regarding Shift Operators

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i couldn't understand the Shift operator("<<",">>",">>>") mechanism....
can any one plz explin this.....

thanks in Advance
regards
krishna.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First I was afraid too...I also couldn't understand the whole shifting thing..

But its sooo easy once you try and code something..
Maybe you can specify your question?!

First of all, you can only shift integral values (int,long)
(You can shift narrower types too, but the operation is always at least performed on int)

right shift (>> ) shifts the bit-representation of the value to the right and fills the resulting "empty space" on the left side with the value of the most significant bit before the shift (the leftmost), so it's a "signed shift", cause if the leftmost bit was 1 (negative) before the shift, it will be negative after the shift..(value is divided by 2 on a shift by 1)

left shift(<< ) shifts to left, and the right side will be filled with zeros(0)..(value is multiplied by 2 on a shift by 1)

unsigned right shift (>>> ) shifts to right, but always fills the space with zeros..

But maybe you dont understand the whole binary 1 and 0 thing?

Regards,
Flom

[ May 26, 2006: Message edited by: Flom Xanther ]
[ May 26, 2006: Message edited by: Flom Xanther ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Get some feeling from code. The output

 
Krishna Bulusu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u very much flom...

i got one shortcut for "<<"..
let us say int i=4;
inr result=i<<4 is nothing but 4*2 power 4 ie, 4*16=64.

is it true??

regards,
krishna
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right:


let us say int i=4;
int result=i<<4 is nothing but 4*2 power 4 ie, 4*16=64.


But, you need to consider how many bits for one "int":
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic