Q&A
How to subtract two binary numbers
Overview
Binary subtraction A − B can be done by hand with borrowing, but digital systems avoid building separate subtraction circuitry by representing negative numbers in two's complement and computing A − B = A + (−B). Understanding both clarifies how CPUs handle signed arithmetic.
graph TD
A["A - B"] --> B[Method 1: Direct borrow subtraction]
A --> C[Method 2: Two's complement]
C --> C1["Negate B: invert bits, add 1"]
C1 --> C2["Compute A + (-B)"]
C2 --> C3[Discard final carry-out]
style C fill:#1e1b4b,color:#fff
Binary Subtraction Rules (direct method)
Like decimal, but in base 2. The borrow cases:
| A − B | Result | Borrow |
|---|---|---|
| 0 − 0 | 0 | 0 |
| 1 − 0 | 1 | 0 |
| 1 − 1 | 0 | 0 |
| 0 − 1 | 1 | 1 (borrow from next bit) |
The tricky case is 0 − 1: write 1 and borrow 1 from the next higher column (just as 0 − 1 in decimal gives 9 with a borrow).
Worked example: 1011 − 0110 (11 − 6 = 5)
1 0 1 1 (11)
- 0 1 1 0 (6)
---------
Column by column (right→left):
bit0: 1-0 = 1
bit1: 1-1 = 0
bit2: 0-1 → borrow: 1, and bit3 reduced by 1
bit3: (1 borrowed→0) - 0 = 0
Result: 0 1 0 1 = 5 ✓
Two's Complement Method (how computers do it)
Subtraction becomes addition of a negative. To compute A − B:
- Find
−Bin two's complement: invert all bits of B (one's complement), then add 1. - Compute
A + (−B). - Discard the final carry-out beyond the fixed bit width.
Worked example: 1011 − 0110 (11 − 6) in 4 bits
B = 0110
invert B = 1001 (one's complement)
add 1 = 1010 (two's complement = -6 in 4-bit)
A + (-B):
1 0 1 1 (11)
+ 1 0 1 0 (-6)
---------
1 0 1 0 1
^ carry-out (discard, beyond 4 bits)
Result: 0 1 0 1 = 5 ✓
The carry-out indicates the result is positive (no borrow needed). For signed interpretation, the most significant bit is the sign bit.
In Code
Most languages just use the - operator (the hardware handles two's complement):
a = 0b1011 # 11
b = 0b0110 # 6
print(bin(a - b)) # 0b101 (5)
int a = 0b1011, b = 0b0110;
System.out.println(Integer.toBinaryString(a - b)); // 101
To implement it manually (educational):
def binary_subtract(a, b, bits=8):
# two's complement of b
neg_b = ((~b) + 1) & ((1 << bits) - 1)
return (a + neg_b) & ((1 << bits) - 1) # discard carry-out
Handling Negative Results
If B > A, direct subtraction needs a borrow out of the most significant bit, and the two's-complement result represents a negative number (its MSB is 1). Interpret it by taking the two's complement again to find the magnitude.
Why This Matters in Interviews
This tests low-level/CS-fundamentals understanding: the borrow rules of direct subtraction, and - more importantly - why computers use two's complement (it makes subtraction reuse the adder circuit, and gives a single representation of zero). Be able to compute −B (invert + add 1) and explain discarding the carry-out. The two's-complement insight is the high-value content.
Common Pitfalls
- Forgetting to add 1 when forming two's complement (that's one's complement, off by one).
- Not fixing a bit width, so the carry-out handling is ambiguous.
- Misinterpreting a negative (MSB=1) result as a large positive number.
- Confusing borrow (subtraction) with carry (addition).
References
- Two's complement: https://en.wikipedia.org/wiki/Two's_complement
- Binary arithmetic overview: https://en.wikipedia.org/wiki/Binary_number#Subtraction