Binary Shifting

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:

  1. What binary shifting is.
  2. Why we use binary shifting.
Media Attachments: Presentation Video

1. What Binary Shifting Is

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.

Worked Example 1 – Shifting to the Left

Let’s take the binary number 110 and shift it to the left by 2 places.

1286432168421
0000 0 110
Our initial number, 110, can be seen above.

Shifting our number to the left one place gives us an empty space. We simply put a 0 in the empty space. This gives us 1100.

1286432168421
0000 1 100
We’ve shifted our number to the left and put a 0 in the empty space.

We can now shift the number by another space. Again, we’ll fill the empty space with a 0. This gives us 11000.

1286432168421
0001 1 000
We’ve shifted our number to the left again and put another 0 in the empty space.

So, all we’ve done is add two 0’s to the right-hand side.

We’ll now look at how a binary shift to the right-hand side works.

Worked Example 2 – Shifting to the Right

Let’s use the same binary number, 110. This time we’ll shift it to the right by 1 place.

1286432168421
0000 0 110
Our initial number, 110, can be seen above.

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.

1286432168421
0000 0 011
We’ve shifted our number to the right and knocked a 0 off the end.

But what if we wanted to shift to the right again? What would happen with the 1? Well, the 1 would be shifted off the end and replaced with a 0 on the left of our binary sequence.

1286432168421
0000 0 001
We’ve shifted our number to the right and knocked a 1 off the end.

Further Thought

What do you think would happen if we did a binary left shift and a 1 was pushed off the left-hand side?

2. Why We Use Binary Shifting

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.

1286432168421
0000 0 110

We then shifted it 1 place to the left which gave us 1100 which is 12 in decimal.

1286432168421
0000 1 100

6 x 2 = 12!

We then shifted it another place to the left which gave us 11000, which is 24 in decimal.

1286432168421
0001 1 000

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.

1286432168421
0000 0 110

Then we shift it one place to the right and we get 11, which is 3 in decimal.

1286432168421
0000 0 011

6 ÷ 2 = 3!

Of course, if we shifted it to the right again, we’d be left with 1 (both in binary and decimal). 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 it’s discarded.

Further Thought

How would we multiply or divide by numbers other than 2?

(HINT: there is no divide or multiply operation inside the CPU.)

Lesson Summary

So to summarise what we’ve learnt in this lesson:

  • 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.