LearnTronics
Math
Binary Arithmetic
We already know how to read binary numbers. Now we can do arithmetic with them. The familiar ideas are the same as in decimal arithmetic: addition, carrying, subtraction, borrowing, and multiplication. The difference is that every digit can only be 0 or 1.
Binary addition
| A | B | Sum bit | Carry | Written result |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 1 | 10 |
Worked example 1 — 5 + 3
Worked example 2 — 11 + 6
Checking in decimal: 11 + 6 = 17, and 10001₂ is 17.
A carry input makes three bits
Once a carry is present, a column may contain three 1s:
Write 1 in the current column and carry 1 into the next.
| A | B | Carry in | Sum | Carry out |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
This is also the behavior of a digital full adder: a circuit that adds two data bits plus a carry from the previous column.
Binary subtraction
| Operation | Result |
|---|---|
| 0 − 0 | 0 |
| 1 − 0 | 1 |
| 1 − 1 | 0 |
| 0 − 1 | Borrow from the next column |
Worked example 3 — 7 − 3
Worked example 4 — borrowing through zeros: 8 − 1
The rightmost 0 cannot subtract 1, so the borrow travels left through the zeros until it reaches the 1 in the 8s position.
Binary multiplication
Binary multiplication is simple because each multiplier digit is either 0 or 1.
| A | B | A × B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Worked example 5 — 5 × 3
Shifting left and right
| Operation | Example | Unsigned whole-number effect |
|---|---|---|
| Left one place | 0011 → 0110 | Multiply by 2 |
| Left two places | 0011 → 1100 | Multiply by 4 |
| Right one place | 1100 → 0110 | Divide by 2 |
Fixed width and overflow
A 4-bit unsigned register can represent:
That is decimal 0 through 15.
Worked example 6 — 4-bit overflow
The mathematical answer needs five bits. If the storage location has only four, the extra leftmost bit is a carry out.
Try these yourself
Problem 1
Show answer
5 + 2 = 7.
Problem 2
Show answer
7 + 1 = 8.
Problem 3
Show answer
10 + 7 = 17.
Problem 4
Show answer
13 − 5 = 8.
Problem 5
Show answer
16 − 1 = 15.
Problem 6
Show answer
6 × 3 = 18.
Where we go next
So far these are unsigned binary numbers. A later lesson can introduce signed binary and two's complement, which allows negative values and makes subtraction especially convenient in digital hardware.