Overview
This worksheet shows how a computer multiplies two binary numbers. You will start with the very small times table for single bits, see why multiplying by 2 is just a shift, then use the shift and add method to multiply any two binary numbers by hand. You will also work out how many bits an answer needs, see what happens when it does not fit, and multiply negative numbers stored in two's complement. There is no programming in this worksheet. Work through it with a pen and paper, and use the interactive diagrams and the Check Answers buttons to test yourself as you go.
Learning Objectives
By the end of this lab you will be able to:
- Multiply a binary number by 2, 4, 8 and 16 using a left shift
- Multiply two binary numbers by hand using the shift and add method
- Work out how many bits are needed to hold the product of two binary numbers
- Recognise when a product is too big to fit in 8 bits
- Multiply two's complement numbers using the sign rule
Concept
A processor has an adding circuit, but multiplying is harder. Multiplying 13 by 11 by adding 13 eleven times would be far too slow for large numbers, so computers need a quicker method that still only uses simple steps.
Binary makes this easy because each digit of the number you multiply by, the multiplier, is only ever 0 or 1. Multiplying by 0 gives 0 and multiplying by 1 leaves the number unchanged. Moving every bit one place to the left, called a left shift, doubles a number, in the same way that adding a 0 to the end of a denary number multiplies it by 10. Put together, this gives the shift and add method. For every 1 in the multiplier, write down the other number, the multiplicand, shifted left by that bit's position. For every 0, write nothing (or a row of zeros). Each of these rows is called a partial product. Adding up the partial products gives the product.
This is the same long multiplication you learned for denary numbers, but simpler, because you never need to know any times table beyond 0 and 1. Multiplying an n-bit number by an m-bit number can give a product up to n + m bits long. If the computer only has room for fewer bits, the extra bits are lost and the stored answer is wrong. This is called overflow.
Tasks
Task 1 - The single bit times table
In denary you had to learn times tables up to 9 × 9. In binary there are only two digits, so the whole times table fits in four lines.
| Sum | Answer |
|---|---|
| 0 × 0 | 0 |
| 0 × 1 | 0 |
| 1 × 0 | 0 |
| 1 × 1 | 1 |
The answer is only 1 when both bits are 1. That makes multiplying two single bits the same as the logical AND operation.
It also means multiplying a whole binary number by a single bit is easy. Multiplying by 0 gives all zeros. Multiplying by 1 gives the same number back.
1 × 1 =
1 × 0 =
1011 × 1 =
1011 × 0 =
Task 2 - Multiplying by 2 is a shift
In denary, multiplying by 10 moves every digit one place to the left and puts a 0 on the end. 47 × 10 = 470.
Binary works the same way, but each column is worth 2 times the column to its right, not 10 times. So moving every bit one place left doubles the number. This is called a left shift.
| Number | Binary | Denary |
|---|---|---|
| Start | 00000101 | 5 |
| Shift left 1 place (× 2) | 00001010 | 10 |
| Shift left 2 places (× 4) | 00010100 | 20 |
| Shift left 3 places (× 8) | 00101000 | 40 |
Shifting left by k places multiplies by 2k.
Try it on the shifter below. It starts at 3. Press Shift left a few times and watch the value double each time. Keep going until a 1 falls off the left-hand end, and see what happens to the value. You can also click any bit to change it, and try Shift right to see the opposite effect.
Write each answer as an 8-bit binary number.
00000101 × 2 (shift left 1 place) =
00000101 × 4 (shift left 2 places) =
00000101 × 8 (shift left 3 places) =
00001101 × 4 (shift left 2 places) =
00000011 × 16 (shift left 4 places) =
00011001 × 2 (shift left 1 place) =
In the shifter, start from 3 and keep shifting left. What happens to the value once a 1 falls off the left-hand end, and why?
The value stops doubling and becomes much smaller, because the 1 that fell off was worth 128 (or more) and is lost. For example 192 × 2 should be 384, but 8 bits can only hold up to 255, so only 128 is left. The answer was too big to fit in 8 bits, which is overflow.
Task 3 - Long multiplication in denary
Before multiplying in binary, remind yourself how long multiplication works in denary. Here is 23 × 14.
2 3
× 1 4
-------
9 2 (23 × 4)
+ 2 3 0 (23 × 1, shifted one place left)
-------
3 2 2
You multiply the top number by each digit of the bottom number in turn, starting on the right. Each new row is shifted one more place to the left, which is why the grey placeholder 0 is there. Then you add the rows.
The hard part in denary is the first step, working out 23 × 4. In binary that step disappears, because every digit of the bottom number is either 0 or 1, and you already know from Task 1 that multiplying by 0 or 1 is trivial.
Task 4 - The shift and add method
To multiply two binary numbers, follow these steps.
- Write the first number (the multiplicand) above the second number (the multiplier).
- Look at the multiplier one bit at a time, starting from the right.
- If the bit is 1, write down a copy of the multiplicand. If the bit is 0, write a row of zeros.
- Shift each new row one more place to the left than the row above it. Fill the gap on the right with placeholder zeros.
- Add up all the rows. These rows are called partial products.
Worked example: 1011 × 0110 (11 × 6)
1 0 1 1 (11)
× 0 1 1 0 (6)
---------------
0 0 0 0 (bit is 0, write zeros)
1 0 1 1 0 (bit is 1, copy the top number, shifted 1 place left)
1 0 1 1 0 0 (bit is 1, copy the top number, shifted 2 places left)
+ 0 0 0 0 0 0 0 (bit is 0, write zeros, shifted 3 places left)
---------------
0 1 0 0 0 0 1 0 = 66
Check in denary. 64 + 2 = 66, and 11 × 6 = 66. Correct.
Ignoring the rows of zeros, the partial products are 22 (11 × 2) and 44 (11 × 4), so the method has really worked out 11 × 6 as 11 × 4 + 11 × 2. That is because 6 is 4 + 2, which is exactly what its binary pattern 0110 says.
In the shift and add method, what do you write down when the multiplier bit is 1?
Why is each partial product shifted one more place left than the one above it?
Task 5 - Step through shift and add
The diagram below carries out shift and add one step at a time on two 4-bit numbers. It starts with the worked example from Task 4, 11 × 6. Press Next step to use the next bit of B, starting from the right. The gold box shows which bit of B has just been used. Grey digits are placeholder zeros.
Once you are happy with how it works, click the bits of A and B to make your own sums. Work each one out on paper first, then step through to check.
- Set B to
0001. What do you notice about the answer? - Set B to
1000. How does the answer relate to A? - Set A and B both to
1111. How many bits does the answer use?
Question 2 above. What happens when B is 1000, and why?
The answer is A shifted left by 3 places, which is A × 8. B has only one 1, in the 8s column, so there is only one partial product. It is a copy of A shifted 3 places left. Every other row is zeros.
Task 6 - A guided multiplication
Work out 1101 × 0101 on paper using shift and add. Fill in each partial product as an 8-bit number, including any placeholder zeros on the right and leading zeros on the left.
For example, if the top number were 0011 and the bit were 1 with a shift of 2, the partial product would be 00001100.
Partial product for bit 0 of the multiplier (the bit is 1) =
Partial product for bit 1 of the multiplier (the bit is 0) =
Partial product for bit 2 of the multiplier (the bit is 1) =
Partial product for bit 3 of the multiplier (the bit is 0) =
Add the partial products. The 8-bit answer is which is in denary
Task 7 - Adding the partial products safely
When there are several partial products, a column can end up with three or four 1s in it. 1 + 1 + 1 = 3 = 11, so you write 1 and carry 1. With four 1s you get 4 = 100, which carries into the column two places to the left. This is easy to get wrong.
A safer way is to keep a running total. Add the first two partial products, then add the next one to that answer, and so on. Then you never add more than two rows at once. This is also how a simple processor does it, using its normal adding circuit once for each bit of the multiplier.
Worked example: 1101 × 1011 (13 × 11)
The multiplier 1011 has 1s in bits 0, 1 and 3, so the partial products are 13, 26 and 104.
0 0 0 0 1 1 0 1 (13, bit 0) + 0 0 0 1 1 0 1 0 (26, bit 1) --------------- 0 0 1 0 0 1 1 1 = 39
Bit 2 of the multiplier is 0, so there is nothing to add for it. Now add the bit 3 partial product to the running total.
0 0 1 0 0 1 1 1 (running total) + 0 1 1 0 1 0 0 0 (104, bit 3) --------------- 1 0 0 0 1 1 1 1 = 143
13 × 11 = 143. Correct.
Use shift and add, with a running total if it helps. Give each answer as an 8-bit binary number and in denary.
0101 × 0011 = which is in denary
0111 × 0110 = which is in denary
1001 × 0101 = which is in denary
1100 × 1010 = which is in denary
1101 × 1011 = which is in denary
1111 × 1111 = which is in denary
Task 8 - Multiplying by numbers that are not powers of 2
Shift and add also explains a trick that computers use when they multiply by a fixed number. To work out x × 10, split 10 into powers of 2. 10 = 8 + 2, so x × 10 = x × 8 + x × 2. That is one shift of 3 places, one shift of 1 place, and one addition.
Worked example: 13 × 10
| 13 | 00001101 |
| 13 × 8 (shift left 3) | 01101000 |
| 13 × 2 (shift left 1) | 00011010 |
0 1 1 0 1 0 0 0 (13 × 8) + 0 0 0 1 1 0 1 0 (13 × 2) --------------- 1 0 0 0 0 0 1 0 = 130
7 × 10. Split 10 into powers of 2: 10 = +
7 × 8 (shift left 3) =
7 × 2 (shift left 1) =
Add them. 7 × 10 = which is in denary
9 × 5. Split 5 into powers of 2: 5 = +
9 × 4 (shift left 2) =
9 itself =
Add them. 9 × 5 = which is in denary
6 × 12. Split 12 into powers of 2: 12 = +
6 × 8 (shift left 3) =
6 × 4 (shift left 2) =
Add them. 6 × 12 = which is in denary
Task 9 - How many bits does the answer need?
When you add two 4-bit numbers, the answer needs at most 5 bits. Multiplying makes numbers grow much faster. The biggest 4-bit number is 1111 = 15, and 15 × 15 = 225 = 11100001, which needs all 8 bits.
In general, multiplying an n-bit number by an m-bit number gives a product that fits in n + m bits. The biggest n-bit number is 2n - 1, so the biggest possible product is the one below, which is always smaller than 2n+m.
This is why many processors put the result of multiplying two 8-bit numbers into a 16-bit space, and the result of multiplying two 32-bit numbers into a 64-bit space.
A 4-bit number multiplied by a 4-bit number needs up to bits
An 8-bit number multiplied by an 8-bit number needs up to bits
A 3-bit number multiplied by a 5-bit number needs up to bits
The biggest product of two 8-bit numbers is 255 × 255 =
The biggest product of a 3-bit number and a 5-bit number is × =
Task 10 - When the product does not fit
Sometimes a computer multiplies two numbers but only has room for an 8-bit answer. If the true product is bigger than 255, the bits above the 128 column are lost, just like the 1 that fell off the end of the shifter in Task 2. This is overflow, and the stored answer is wrong.
Worked example: 20 × 13
The true answer is 260 = 0000000100000100 in 16 bits. Keep only the rightmost 8 bits and you get 00000100, which is 4. The 256 has been lost.
A quick check is to work out the product in denary first. Anything above 255 will overflow an 8-bit space.
20 × 13. The true answer is
Keeping only 8 bits, the stored pattern is which reads as
16 × 16. The true answer is
Keeping only 8 bits, the stored pattern is which reads as
30 × 9. The true answer is
Keeping only 8 bits, the stored pattern is which reads as
Which of these products fits in an unsigned 8-bit number?
Task 11 - Multiplying two's complement numbers
In the Two's Complement lab you stored negative numbers using a -128 column, and found -x with flip and add 1. The easiest way to multiply signed numbers by hand is to deal with the signs separately, using the same sign rule as in denary.
| Signs | Answer is |
|---|---|
| positive × positive | positive |
| positive × negative | negative |
| negative × positive | negative |
| negative × negative | positive |
- Note whether the answer will be positive or negative.
- Turn any negative number into its positive version with flip and add 1.
- Multiply the two positive numbers using shift and add.
- If the answer should be negative, flip and add 1 on the product.
Worked example: 11111101 × 00000101 (-3 × 5)
- One number is negative, so the answer is negative.
11111101is negative. Flip gives00000010, add 1 gives00000011, which is +3.- 3 × 5 = 15 =
00001111. - The answer should be negative. Flip gives
11110000, add 1 gives11110001.
Check with the -128 column: -128 + 64 + 32 + 16 + 1 = -15. Correct.
Give each answer in 8-bit two's complement and in denary. Remember the minus sign for negative answers, for example -7.
11111100 × 00000110 = which is in denary
00000101 × 11111010 = which is in denary
11111001 × 11111101 = which is in denary
11110111 × 00001000 = which is in denary
What is the sign of the product of 11111001 and 11111100 in two's complement?
Task 12 - Check your understanding
Shifting a binary number left by 3 places has the same effect as
How many partial products that are not all zeros does 1010 × 0101 have?
What is 0111 × 0011?
A processor multiplies two 16-bit numbers. How many bits should it allow for the answer so that it never overflows?
Explain why multiplying in binary is simpler than multiplying in denary.
Each digit of the multiplier is only 0 or 1. Multiplying by 0 gives zero and multiplying by 1 gives the same number, so you never need a times table. Every partial product is either a row of zeros or a shifted copy of the multiplicand, and the only real work is adding them up.
Explain how a computer could multiply a number by 6 using only shifts and one addition.
6 = 4 + 2. Shift the number left by 2 places to get the number × 4, and shift it left by 1 place to get the number × 2. Adding these two results gives the number × 6.