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:

- What binary shifting is.
- Why we use binary shifting.

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.

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

128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|

0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |

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.

128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|

0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |

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 |

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.

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

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 |

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.

128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|

0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |

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

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.

128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|

0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |

Then we shift it one place to the right and we get 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!

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.

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

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

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.

Quizzes

Send this to a friend