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.
user unknown Jun 17 '11 at 12:42 on 2011-06-17 12:42
source share