One of the things we can do with binary data is shift the number of bits left or right.
This gives us a very quick way of multiplying or dividing any binary number by 2.
In this lesson, we’ll learn about:
Binary shifting is where we take any binary number and then shift it to the left or the right.
We then replace the empty space with a 0.
We can see this in action by demonstrating a binary shift to the left.
Binary Shifting to the Left
Let’s take the binary number 110 and shift it to the left by 2 places.
Our initial number, 110, can be seen below.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
Shifting our number to the left one place leaves an empty space. We simply put a 0 in the empty space, which gives us 1100.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
1100 in decimal is 12, which is double 110, which is 6 in decimal.
We can now shift the number by another space. Again, we’ll fill the empty space with a 0. This gives us 11000.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 |
11000 in decimal is 24, which is double 1100, which is 12 in decimal.
Let’s now take our initial number, 110, and instead shift it to the right.
Our initial number, 110, can be seen below.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
Shifting to the right will knock off the far right-hand bit, leaving us with 11.
We’ll make sure we add a 0 to the left-hand side of the number.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
11 in decimal is 3, which is half 110, which is 6 in decimal.
Shifting in binary is a very quick method of multiplying or dividing any number by 2 every time you shift one place.
Let’s look at our first example.
We had the binary number 110, which in decimal is 6.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
We then shifted it 1 place to the left, which gave us 1100, which is 12 in decimal.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
6 x 2 = 12!
We then shifted it another place to the left, which gave us 11000, which is 24 in decimal.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 |
12 x 2 = 24!
Similarly, shifting to the right allows us to divide by 2 for each shift.
Again, we start with 110, which is 6, and then we shift it one place to the right. This gives us 11, which is 3 in decimal.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
6 ÷ 2 = 3!
What would happen, though, if we then shifted to the right one more time?
We’d get 1 (which is also 1 in decimal).
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
We know that 3 ÷ 2 = 1.5, not 1.
The reason this has happened is because in this form of binary we’re not storing decimals, so the fraction is discarded.
Binary shifting is when you move a binary number to the left or right.
When shifting to the left, we add a new 0 onto the right-hand side of our binary sequence.
When shifting to the right, we add a new 0 onto the left-hand side of our binary sequence.
If you shift to the left, this is the same as multiplying your number by 2.
If you shift to the right, this is dividing your number by 2.