Beetwise

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:

  1. Bit 0: 0 less 1 needs a borrow. Borrow from bit 1, so bit 0 gives 1.
  2. Bit 1: 0 after the borrow, less 1, needs another borrow. Bit 1 gives 1.
  3. Bit 2: 0 after the borrow, less 0, gives 0.
  4. Bit 3: 1 less 0 gives 1.
  5. The result is 0111, which is 7.

The result of one column of a subtraction

The result of one column of a subtraction
ABBorrow inResult bitBorrow out
00000
10010
01011
11000
00111
11111

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 editor

Questions 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.