Binary subtraction
Binary subtraction works one position at a time, from the right.
A position that needs more than it holds borrows from the next position up.
The value 1010 less 0011 gives 0111, which is 7.
Try binary subtraction in the bit editor
The difference of 1010 and 0011. Edit the expression or select a bit to flip it.
7
6
5
4
3
2
1
0
7
value · 8 bits · unsigned
- binary
- 0b00000111
Binary subtraction step by step
Calculate 1010 less 0011:
- Bit 0: 0 less 1 needs a borrow. Borrow from bit 1, so bit 0 gives 1.
- Bit 1: 0 after the borrow, less 1, needs another borrow. Bit 1 gives 1.
- Bit 2: 0 after the borrow, less 0, gives 0.
- Bit 3: 1 less 0 gives 1.
- The result is 0111, which is 7.
The result of one column of a subtraction
| A | B | Borrow in | Result bit | Borrow out |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 | 1 |
Where binary subtraction is used
A processor subtracts through its adder, with the two’s complement of the second value.
A timer that counts down runs a subtraction on every tick.
A difference of two sensor readings shows the drift between them.
Binary subtraction: points to note
- A result below zero wraps. At a bit width of 8, 3 less 5 gives 254, which reads as -2 in signed mode.
- An unsigned compare after a wrap gives the wrong answer, because 254 is larger than 3.
- Beetwise widens the bit width to hold a result, so set the width to see the wrap.
- A borrow out of the top position disappears, exactly as a carry does.
The full bit editor
The full editor holds many values, evaluates expressions across them, and reads live values from a serial port.
Open this value in the full editorQuestions about binary subtraction
- What is 1010 less 0011 in binary?
- The result is 0111, which is 7 in decimal.
- What happens when the result is below zero?
- The value wraps. At a bit width of 8, 3 less 5 gives 254, which is -2 as a signed value.
- How does a processor subtract?
- It adds the two’s complement of the second value, so one adder circuit covers both operations.