# utility of left and right shift operator

This is a discussion on utility of left and right shift operator within the C++ Programming forums, part of the General Programming Boards category; Hi ! I am confused of utility of left and right shift operator. Since left only multiply by 2 and ...

1. ## 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.

bhagwat

2. Signed values may do screwy things, like not multiply or divide by 2.

3. ## can't understand

sory Dave!

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

thanks
bhagwat

4. 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.

5. 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. 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.

7. 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).

8. 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.

9. 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).