Thread: utility of left and right shift operator

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    utility of left and right shift operator

    Hi !

    I am confused of utility of left and right shift operator.

    Since left only multiply by 2 and right divide by 2.

    this thing we can do with normal / and * operator .

    My question is that where the significance on left and right shift operator lies in programming

    over normal divide and multiply operator.

    Thanks in advanced.


    bhagwat

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Signed values may do screwy things, like not multiply or divide by 2.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    can't understand

    sory Dave!

    I can't understand can u plz explain in easy language and with example

    thanks
    bhagwat

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Probably not.

    It depends on your platform -- which means there is a 99% chance you will not notice anything. But there may be a difference in the language itself that supports different representations of signed values and implementation-defined results of right-shifting negative signed values.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    These are bitwise operations, and they are mainly meant for cases where you don't treat the value as a mathematical object, but are more interested in the bit pattern (binary representation). In which case, I think, one should always use unsigned values.

    For example, the bit patterns could represent the positions of buttons in a checkers board.

    So, in mathematical context I would prefer * and /. But it makes less sense to write white_buttons *16 than white_buttons << 4 (the latter moves the buttons one row up)

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Also, x * 16 is slower than x << 4 on most platforms. There is really no point in actually trying this since the compiler will probably optimize it for you in advance.

    As for what anon mentioned, look up bitboards.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Since left only multiply by 2 and right divide by 2.... My question is that where the significance on left and right shift operator lies in programming...
    One example would be in serial communications. With serial data, you are sending/receiving one bit at a time, and it needs to be shifted out/into a parallel register in order to be used as regular parallel data in a computer.

    In the real world this is going to be done in hardware/firmware, so the average C/C++ programmer doesn't have to worry about the parallel-to-serial details.

    In my hardware world, we work with binary data every day. Like anan said, the binary number usually represends a bit pattern... We aren't usually concerned with the numerical value. Of all the bitwise operations, I'd say that bit-shifting is the least used! Sometimes we use what we call a "walking one" or a "walking zero" to test an address bus or a data bus. It's a quick and easy way to find an open or shorted address or data line.

    While on the subject of bit shifting and bitwise operations... When we are working with binary, we normally use hexadecimal. Its easy to convert between binary and hex (much easier than converting between binary and decimal). You can learn to do it in your head. It's easier to read/write or say a hex number than an binary number (especially when you get beyond 8 bits). And, hex is built into cin and cout (binary is not).
    Last edited by DougDbug; 12-12-2006 at 03:29 PM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Not all values align perfectly on byte boundaries, either; especially file formats are often tightly packed. Try to decode a Flash movie without bit shifting and see how far you'll get.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    One thing that bitwise operations can do is divide and multiply by _powers_ of two. Not constant multiples, but non-constant powers. It's more efficient to compute x << y than it is to compute x * integer_power(2,y).
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

Popular pages Recent additions subscribe to a feed