What makes a bit shift (left or right) and what is it used for?

I saw the >> and << operators in the different code that I was looking at (nothing that I really didnโ€™t understand), but Iโ€™m just wondering what they actually do and what practical applications they are.

EDIT

If the shifts are similar to x * 2 and x / 2 , what is the real difference from the actual use of the * and / operators? Is there a difference in performance?

+52
bit-manipulation bit-shift bitwise-operators
Jun 17 '11 at 12:35
source share
9 answers

Here is an applet where you can perform some bit operations, including a shift.

You have a set of bits, and you move some of them out of bounds:

 1111 1110 << 2 1111 1000 

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

 0001 1111 >> 3 0000 0011 

Fill in the left. A leading case is a special case. It often indicates a negative value, depending on the language and data type. So often you want that if you move to the right, the first bit remains the way it is.

 1100 1100 >> 1 1110 0110 

and it persists with several shifts:

 1100 1100 >> 2 1111 0011 

If you do not want the first bit to be saved, you use (in Java, Scala, C ++, C afaik, and possibly more) the operator with a triple sign:

 1100 1100 >>> 1 0110 0110 

In the other direction there is no equivalent, because it does not make sense - maybe in your particular context, but not in general.

Mathematically, a left shift is * = 2, 2 left shifts are * = 4, etc. The right shift is / = 2, etc.

+39
Jun 17 '11 at 12:42 on
source share

Left bit offset for multiplying by any power of two and right bit offset for dividing by any power in 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 equals 8). Similarly x = x / 2; there is x>>1 , etc.

+28
Jun 17 '11 at 12:39 on
source share

Left shift

x = x * 2^value (normal operation)

x << value (bitwise operation)




x = x * 16 (which coincides with 2^4 )

left shift equivalent will be x = x << 4

Right shift

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

x >> value (bitwise operation)




x = x / 8 (which coincides with 2^3 )

Right shift equivalent will be x = x >> 3

+17
Jul 29 '15 at 10:28
source share

Left shift : it is equal to the product of the value to be shifted, and 2 is increased to the value of the 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 

Shift to the right : it is equal to the quotient, which should be shifted by 2, raised to the degree of offset of the number of bits.

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 
+10
Apr 04 '16 at 0:30
source share

Bit shift operators are more efficient than the / or * operator. In computer architecture, divide (/) or multiply (*) by more than 1 unit of time and register to calculate the result, while the bit change operator is just one register and one unit calculation.

+3
Jul 22 '15 at 8:43
source share

Some examples:

  • Bit operations, such as converting to and from base64 (which is 6 bits instead of 8)
  • 2 operations ( 1 << 4 equals 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.
+2
Jun 17 '11 at 12:37
source share

Yes, I believe that performance can be different, since bitwise operations with left and right shifts can be performed with complexity o (1) with a huge data set.

For example, calculating the power 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++; } } 

A similar bitwise left shift code would look like this:

 value = 1 << n; 

In addition, performing a bitwise operation is like an exact replica of user level mathematical operations (which are machine level end instructions processed by the microcontroller and processor).

+1
Jan 21 '15 at 19:09
source share

Move the left bit to multiply by any power in two. The right bit offset for dividing by any power is doubled.

 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); 
+1
Apr 05 '17 at 16:30
source share

Here is an example:

 #include"stdio.h" #include"conio.h" void main() { int rm,vivek; clrscr(); printf("enter the any numbers\t(eg)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(); } 
0
Oct 07 '14 at 7:56
source share



All Articles