Binary Subtraction Calculator
Subtract two binary numbers and see every borrow column by column, then the same subtraction done as two's complement addition with carry, borrow and overflow flags.
11177Method 1: column subtraction with borrows
borrow 111
1010
− 0011
────
= 0111| Bit | Top | Bottom | Borrow in | Working | Digit | Borrow out |
|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 0 + 2 − 1 − 0 = 1 | 1 | 1 |
| 1 | 1 | 1 | 1 | 1 + 2 − 1 − 1 = 1 | 1 | 1 |
| 2 | 0 | 0 | 1 | 0 + 2 − 0 − 1 = 1 | 1 | 1 |
| 3 | 1 | 0 | 1 | 1 − 0 − 1 = 0 | 0 | 0 |
Method 2: two's complement, the way a CPU does it
carry 10001
A 1010
~B + 1100
+1 + 1
────
10111 (carry out discarded)Invert every bit of B and add 1 to get −B (1101), then add it to A. The adder's carry-in supplies the +1, so subtraction needs no separate circuit - see the binary adder for the gates.
| Reading | A | B | Register result |
|---|---|---|---|
| Unsigned | 10 | 3 | 7 |
| Signed | -6 | 3 | 7 |
No borrow: the unsigned result is exact. The signed difference -9 does not fit in 4 bits, so the signed reading is wrong - that is the overflow flag. Read any pattern as signed on the two's complement calculator.
Binary subtraction rules
| Column | Difference | Borrow |
|---|---|---|
| 0 − 0 | 0 | 0 |
| 1 − 0 | 1 | 0 |
| 1 − 1 | 0 | 0 |
| 0 − 1 | 1 | 1 (take 2 from the next column) |
A column that owes a borrow subtracts one more. When the next column to the left is 0 it cannot lend, so it borrows in turn, and the borrow runs left until it reaches a 1 - which is why 10000 − 1 = 01111. For addition, multiplication and division of binary numbers use the binary calculator.
Code Examples
a, b = 0b1010, 0b0011
bin(a - b) # '0b111'
# 8-bit two's complement: A + (~B + 1), keep 8 bits
width = 8
mask = (1 << width) - 1
result = (a + (~b & mask) + 1) & mask
borrow = a < bFrequently Asked Questions
How do you subtract binary numbers?
Work right to left like decimal subtraction. 0-0 = 0, 1-0 = 1, 1-1 = 0, and 0-1 = 1 with a borrow from the next column. A borrowed column then subtracts one extra. For example 1010 - 0011 = 0111 (10 - 3 = 7).
What is 0 - 1 in binary?
1, borrow 1. The column takes 2 (binary 10) from the column to its left, and 10 - 1 = 1. If no column to the left has a 1 to lend, the result is negative.
How do computers subtract binary numbers?
They add the two's complement. A - B is computed as A + (~B + 1): invert every bit of B, then add with a carry-in of 1. The same adder circuit does both addition and subtraction, and the result wraps at the register width.
What happens when the answer is negative?
By hand, swap the operands, subtract, and write a minus sign: 0011 - 1010 = -0111. In a fixed-width register the result wraps to a two's complement pattern instead: 3 - 5 in 8 bits is 11111110, which reads as -2 when signed.
What are the borrow and overflow flags?
Borrow means the unsigned subtraction went below zero (A < B); the adder shows it as no carry out of the top bit. Overflow means the signed result does not fit: -128 - 1 in 8 bits wraps to +127. They are separate questions about the same bit pattern.
Is the carry flag set or clear after a borrow?
It depends on the CPU. x86 sets CF when a subtraction borrows. ARM sets C to the adder's carry out, which is the inverse: C = 1 means no borrow. This page shows both the raw carry out and the borrow.
Related Tools
This tool runs entirely in your browser - nothing you enter is uploaded or stored.