What does a bitwise shift (left or right) do and what is it used for?

喜夏-厌秋 提交于 2019-11-28 02:51:37
user unknown

Here is an applet where you can exercise some bit-operations, including shifting.

You have a collection of bits, and you move some of them beyond their bounds:

1111 1110 << 2 
1111 1000 

it is filled from the right with fresh zeros. :)

0001 1111 >> 3 
0000 0011 

filled from left. A special case is the leading 1. It indicates often a negative value - depending on the language and datatype. So often it is wanted, that if you shift right, the first bit stays as it is.

1100 1100 >> 1
1110 0110 

and it is conserved over multiple shifts:

1100 1100 >> 2
1111 0011

If you don't want the first bit to be preserved, you use (in Java, Scala, C++, C afaik, and maybe more) a triple-sign-operator:

1100 1100 >>> 1
0110 0110 

There is no equivalent in the other direction because it makes no sense - maybe in your very special context, but not in general.

Mathematically, a left-shift is a *=2, 2 left-shifts is a *=4 and so on. A right-shift is a /= 2 and so on.

Nitish

Left bit shifting to multiply by any power of two and Right bit shifting to divide by any power of two. For example x = x * 2; can also be written as x<<1 or x = x*8 can be written as x<<3 (since 2 to the power of 3 is 8). Similarly x = x / 2; is x>>1 and so on.

loyola

Left Shift

x = x * 2^value (normal operation)

x << value (bit-wise operation)


x = x * 16 (which is the same as 2^4)

The left shift equivalent would be x = x << 4

Right Shift

x = x / 2^value (normal arithmetic operation)

x >> value (bit-wise operation)


x = x / 8 (which is the same as 2^3)

The right shift equivalent would be x = x >> 3

Raghu

Left shift : It is equal to product of value which has to be shifted and 2 raised to the power of Number of Bits to be shifted.

Example :

1<<3
0000 0001  ---> 1
Shift by 1 bit
0000 0010 ----> 2 which is equal to 1*2^1
Shift By 2 bits
0000 0100 ----> 4 which is equal to 1*2^2
Shift by 3 bits
0000 1000 ----> 8 which is equal to 1*2^3

Right shift : It is equal to quotient of value which has to be shifted by 2 raised to the power of Number of Bits to be shifted.

Example :

8>>3
0000 1000  ---> 8 which is equal to 8/2^0
Shift by 1 bit
0000 0100 ----> 4 which is equal to 8/2^1
Shift By 2 bits
0000 0010 ----> 2 which is equal to 8/2^2
Shift by 3 bits
0000 0001 ----> 1 which is equal to 8/2^3

The bit shift operators are more efficient as compared to / or * operator. In computer architecture, divide(/) or multiply(*) take more than 1 time unit and register to compute result, while, bit shift operator, is just one one register and one time unit computation.

Left bit shifting to multiply by any power of two. Right bit shifting to divide by any power of two.

x = x << 5; // Left shift
y = y >> 5; // Right shift

In C/C++ it can be written as,

#include <math.h>

x = x * pow(2, 5);
y = y / pow(2, 5);

Some examples:

  • Bit operations for example converting to and from base64 (which is 6 bits instead of 8)
  • doing power of 2 operations (1 << 4 equal to 2^4 i.e. 16)
  • Writing more readable code when working with bits. For example, defining constants using 1 << 4 or 1 << 5 is more readable.

Yes, I think performance wise you might find a difference as bitwise left and right shift operations can be performed with a complexity of o(1) with huge data set.

For eg Calculating power of 2 ^ n:-

int value = 1;
while (exponent<n)
    {
       //print out current power of 2
        value =value *2; // equivalent machine level left shift bit wise operation
        exponent++;
         }
    }

Similar code with bitwise left shift operation would be like:

value = 1 << n;

Moreover, performing bit-wise operation is like exacting replica of user level mathematical operations (which is the final machine level instructions processed by the micro controller and processor).

R.M.VIVEK Arni

Here is an example:

#include"stdio.h"
#include"conio.h"

void main()
{
    int rm,vivek;
    clrscr();
    printf("enter the any numbers\t(e.g)1,2,5");
    scanf("%d",&rm);//rm=5(0101)<<2(two step add zero's)so,value is 10100
    printf("this lift shitf value%d=%d",rm,rm<<4);
    printf("this right shitf value%d=%d",rm,rm>>2);
    getch();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!