What does a bitwise shift (left or right) do and what is it used for?
Solution 1
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 the left. A special case is the leading 1. It often indicates 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 as far as I know, and maybe more) a triple-sign-operator:
1100 1100 >>> 1
0110 0110
There isn't any equivalent in the other direction, because it doesn't make any 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.
Solution 2
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.
Solution 3
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
Solution 4
Left shift: It is equal to the product of the 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
Solution 5
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);
Comments
-
Per Knytt almost 2 years
I've seen the operators
>>
and<<
in various code that I've looked at (none of which I actually understood), but I'm just wondering what they actually do and what some practical uses of them are.If the shifts are like
x * 2
andx / 2
, what is the real difference from actually using the*
and/
operators? Is there a performance difference?-
Waihon Yew almost 13 yearsGoogling for "bitwise shift" and looking at the first result (Wikipedia) probably isn't that hard. It also answers all of the above.
-
Waqas Shabbir almost 9 yearsYes, off-course there should be a performance difference. Please see this [link] (dotnetperls.com/shift)
-
TylerH over 8 yearsPossible duplicate of What are bitwise shift (bit-shift) operators and how do they work?
-
-
user3391801 almost 13 yearsANSI C defines only the two bitwise shift operators >> and <<.
-
user unknown over 9 years@TML: ANSI C isn't the only language which uses bitwise shift operators. C++ uses them too and Java does, doesn't it? I guess there are even more languages. and I don't ses "C" in the headlinen, nor in the text or tags of the question.
-
user3391801 over 9 yearsNo, the question doesn't; which is why I still upvoted you. But at the time (admittedly, this was almost 4 years ago) I felt it was a valuable comment to add. :)
-
S.S. Anne about 5 yearsDoes it go
2
4
6
8
or2
4
8
16
? -
user unknown about 5 years@JL2210: Don't you have the possibility to try it out? Or to calculate it with pen and paper? Since I wrote *=2, and not +=2, it should be the latter, shouldn't it?
-
S.S. Anne about 5 yearsI think I understand now. I was just wondering because I needed to multiply a pointer by 16.
-
Peter Mortensen almost 4 yearsIsn't there one-cycle multiplication with modern CPUs?