There is a very simple method that can be used to multiply and divide binary numbers. This method is called bit shifting.
Shifting Bits
What is the easiest multiplication that you can do in decimal? Multiplying and dividing by factors of ten is the easiest because all you have to do is add or remove zeroes to or from the original number.
- 12 times 10 is 120
- 12 times 100 is 1200
- 1200 divided by 1000 is 1.2
We can do a similar trick with binary, the only difference is that we will be multiplying or dividing by factors of two (not ten).
Lets start with the binary value for three, 11.
We can multiply this value by two if we add a zero to the end, like this. 110.
Three has now become six!
Binary Left Shifts
If we wanted to we can multiply by four by adding two zeroes (11 becomes 1100: three times four is eight), if we want to multiply by eight we can add three zeroes (11 becomes 11000: three times eight is sixteen)and so on….
We call this a binary left shift because we move the original number to the left and we fill in the spaces left behind with zeroes.
Binary Right Shifts
We can use the same trick to divide by factors of two.
Let’s take the binary value for one hundred and twenty: 1111000
If we want to divide this number by two we can just remove a value from the end. 1111000 becomes 111100 (120 divided by two is 60)
We can divide by four by removing two values from the end (1111000 becomes 11110: One hundred and twenty divided by four is thirty)
This is called a binary right shift, it can be very useful but it can also cause problems if we are not careful.
Say we had the number 255 in binary : 11111111 and we divided that value by 4 we would get 111111. 255 divided by four is 63? Not quite. There are 63 fours in in 255 and that would leave a remainder of 3. |
Another example, if we had 170 and wanted to divide by 16 in binary this is: 10101010 shifted 4 places to the right becomes 1010: 170 divided by 16 is 10 (remainder 10) |
- In the first example we had 11111111 (255) and we divided that by 4 by removing two digits (11) from the right.
- In the second example we had 10101010 (170) and we divided that by 16 by removing four digits (1010) from the right.
Note that the value that we removed is equal to the remainder part of the division.
- 255 divided by 4 is 63, remainder 3 (11111111 shifted 2 places right is 111111 remainder 11)
- 170 divided by 16 is 10, remainder 10 (10101010 shifted 4 places right is 1010 remainder 1010)
Left and right shifts summarised
Places shifted | Left | Right |
---|---|---|
1 | Multiply by 2 | Divide by 2 |
2 | Multiply by 4 | Divide by 4 |
3 | Multiply by 8 | Divide by 8 |
4 | Multiply by 16 | Divide by 16 |
5 | Multiply by 32 | Divide by 32 |
6 | Multiply by 64 | Divide by 64 |
7 | Multiply by 128 | Divide by 128 |
8 | Multiply by 256 | Divide by 256 |
n | Multiply by 2n | Divide by 2n |