Binary Arithmetic: Adding, Subtracting and Multiplying Bits
Every calculation a processor performs comes down to arithmetic on binary digits. The methods are the same column-by-column procedures you use for decimal numbers. The difference is that each column can hold only 0 or 1, so a column spills over at 2 instead of 10. This lesson works through unsigned addition, subtraction and multiplication by hand. It then shows what happens when a result is too large for the fixed number of bits a computer provides.
Adding column by column
Start at the rightmost column, the LSB, and move left. When a column total reaches 2, you write a 0 and pass a 1 to the next column. That 1 is the carry: the value sent into the next column whenever a column's total is 2 or more. Each column obeys
where ai and bi are the bits in column i, ci is the carry coming in, si is the sum bit you write and ci+1 is the carry going out. Only four column totals are possible:
Column total | Sum bit written | Carry out |
|---|---|---|
0 | 0 | 0 |
1 | 1 | 0 |
2 | 0 | 1 |
3 | 1 | 1 |
Example: to add 10112 and 01102, column 0 gives 1 + 0 = 1. Column 1 gives 1 + 1 = 2, so write 0 and carry 1. Column 2 gives 0 + 1 + 1 = 2, so write 0 and carry 1. Column 3 gives 1 + 0 + 1 = 2, so write 0 and carry 1, which becomes a new leftmost digit. The sum is 100012, and the decimal check 11 + 6 = 17 confirms it.
Subtracting with borrows
Subtraction also runs right to left. When the top bit in a column is smaller than the bit being subtracted plus any borrow already owed, take a borrow from the next column. A borrow is worth 2 in the current column, because the column to the left has twice the place value.
Example: for 11002 − 01012, column 0 is 0 − 1, so borrow: 2 + 0 − 1 = 1. Column 1 is 0 − 0 − 1 for the owed borrow, so borrow again: 2 + 0 − 0 − 1 = 1. Column 2 is 1 − 1 − 1, which is negative, so borrow once more: 2 + 1 − 1 − 1 = 1. Column 3 is 1 − 0 − 1 = 0. The difference is 01112, matching 12 − 5 = 7.
Many processors subtract by adding a complemented value instead of borrowing; that technique belongs with complement representations. By hand, borrowing is the direct method.
Multiplying by shifting and adding
Single-bit products are simple: 1×1=1 and every other product is 0. So each bit of the multiplier contributes either a copy of the multiplicand or nothing. The copy produced by bit k is shifted left k places, and shifting left by k places multiplies by 2k.
Example: for 1012 × 112, bit 0 of the multiplier contributes 1012 and bit 1 contributes 10102. Adding the two partial products gives 11112, which is 5 × 3 = 15. Add partial products two at a time so every column total stays at 3 or less.
Fixed width and overflow
A computer stores numbers in a register with a fixed number of bits n. An unsigned n-bit register holds values from 0 to 2n−1, so 4 bits cover 0 to 15 and 8 bits cover 0 to 255. Overflow occurs when the true result of an operation falls outside that range. For unsigned addition, the register keeps only the lowest n bits:
In the earlier addition, 10112 + 01102 = 100012 needs five bits. A 4-bit register keeps 00012, which is 17 − 16 = 1, and the extra 1 leaves as a carry out of the MSB. Unsigned subtraction overflows in the matching way: a borrow is still owed after the leftmost column, because the true result is negative.
What to remember
- Work right to left; a column total of 2 writes 0 and carries 1, and a total of 3 writes 1 and carries 1.
- A borrow adds 2 to the current column and owes 1 to the next column on the left.
- Multiply by adding shifted copies of the multiplicand, one for each 1 in the multiplier.
- An unsigned n-bit register keeps the result modulo 2n; a carry out of the leftmost bit signals overflow.
- Check every result by converting the inputs and the answer to decimal.
Example: Arithmetic in an 8-bit microcontroller register
A small microcontroller stores whole numbers in 8-bit unsigned registers, so each register can hold any value from 0 to 255. Work each part by hand in binary before checking it in decimal.
a) Add 101101102 and 010111012. Give the full binary sum, the 8 bits the register keeps, and state whether overflow occurs.
Add column by column from bit 0, carrying 1 whenever a column total reaches 2.
- Bits 0 and 1: 0 + 1 = 1 and 1 + 0 = 1, so write 1 in each with no carry.
- Bit 2: 1 + 1 = 2, so write ________ and carry 1.
- Bit 3: 0 + 1 + 1 = 2, so write 0 and carry 1.
- Bit 4: 1 + 1 + 1 = 3, so write ________ and carry 1.
- Bits 5, 6 and 7: each column holds one 1 plus the incoming carry, a total of 2, so each writes 0 and carries 1.
The carry out of bit 7 becomes a ninth digit:
In decimal the inputs are ________ and ________, and 182 + 93 = 275 = 256 + 19. The register keeps only the lowest 8 bits, 000100112=19. Because a carry left bit 7, the addition overflows: the true sum of 275 exceeds the register's maximum of 255.
b) Subtract 011010112 from 110100102 and give the 8-bit difference.
Subtract column by column from bit 0. When a column cannot pay, borrow, which adds 2 to that column and owes 1 to the next column on the left.
- Bit 0: 0 − 1 is negative, so borrow: 2 + 0 − 1 = 1.
- Bit 1: 1 − 1 − 1 for the owed borrow is negative, so borrow: 2 + 1 − 1 − 1 = ________.
- Bit 2: 0 − 0 − 1 is negative, so borrow: 2 + 0 − 0 − 1 = 1.
- Bit 3: 0 − 1 − 1 is negative, so borrow: 2 + 0 − 1 − 1 = ________.
- Bit 4: 1 − 0 − 1 = 0, and nothing is owed to bit 5.
- Bit 5: 0 − 1 is negative, so borrow: 2 + 0 − 1 = 1.
- Bit 6: 1 − 1 − 1 is negative, so borrow: 2 + 1 − 1 − 1 = 1.
- Bit 7: 1 − 0 − 1 = 0, with no borrow left over.
Check in decimal: the inputs are ________ and ________, and the result 011001112=64+32+4+2+1=103, which equals 210 − 107. No borrow is owed after bit 7, so the difference fits the register.
c) Multiply 11012 by 10112 and decide whether the product fits in one 8-bit register.
Each 1 in the multiplier 10112 contributes a copy of 11012 shifted left by that bit's position; the 0 in bit 2 contributes nothing.
- Bit 0 contributes 11012.
- Bit 1 contributes 110102.
- Bit 3 contributes 11010002.
Adding the first two partial products gives ________ in binary, which is 13 + 26 = ________. Now add the third partial product. Bits 0 to 4 never hold two 1s, so they copy down without carries. Bit 5 is 1 + 1 = 2, so write 0 and carry 1; bit 6 is 0 + 1 + 1 = 2, so write 0 and carry 1 into bit 7.
Check in decimal: 13 × 11 = 143 = 128 + 15, which matches 100011112. The product has exactly 8 bits and 143≤255, so it fits in one register without overflow.
Add the 4-bit unsigned numbers 01112 and 01012 by hand. For each bit position, starting from bit 0 (the rightmost bit), enter the sum bit written in that column and the carry passed to the next column. Use 0 or 1 in every cell.
| Bit position | Sum bit | Carry out |
| Bit 0 | ||
| Bit 1 | ||
| Bit 2 | ||
| Bit 3 |