Binary Subtraction Calculator
Subtract one binary number from another with this free binary subtraction calculator. Enter the minuend and subtrahend to calculate the difference, view the decimal equivalent, follow the borrow process bit by bit, and compare the answer with the two's complement method used in digital arithmetic.
Calculations run in your browser. Choose a bit width to inspect fixed-width results, signed two's-complement interpretation and overflow.
Subtract two binary numbers
Binary subtraction results
Exact difference in decimal
Fixed-width binary result
Fixed-width hexadecimal
Unsigned interpretation
Signed two's-complement interpretation
Subtraction using the borrow method
Two's-complement subtraction
Flags and range checks
The four rules of binary subtraction
Binary subtraction works in base 2. When a bit in the minuend is smaller than the corresponding bit in the subtrahend, a borrow may be required from a more significant bit.
borrow 1
In the last case, borrowing from the next column adds a value of
2 to the current column. Therefore, 10₂ − 1₂ = 1₂.
If the next column is also zero, the borrow can propagate through
several columns until a 1 is found.
Worked example: 1010 − 0011
The binary number 1010 is decimal 10, and
0011 is decimal 3. Their difference is decimal 7,
which is 0111 in four bits.
Starting at the rightmost bit, 0 − 1 requires a
borrow. The borrow is propagated through the next column, and
the remaining columns are evaluated using the same four rules.
The calculator displays a column-by-column borrow trace for
the operands you enter.
How two's-complement subtraction works
Digital systems can calculate A − B by adding the two's complement of B to A. To obtain the two's complement, invert every bit of B and add 1. Keep the selected number of low-order bits in the final result.
- Pad B with leading zeros to the selected bit width.
- Invert each bit to get the one's complement.
- Add 1 to obtain the two's complement of B.
- Add that value to A.
- Discard any carry beyond the selected width.
For example, in 4 bits, 1010 − 0011 can be calculated
as 1010 + 1101 = 10111. Discarding the carry beyond
the four-bit result leaves 0111, or decimal 7.
If A is smaller than B, the fixed-width result wraps around modulo 2n. Interpreting that bit pattern as a signed two's-complement integer gives the negative mathematical difference only when the exact result is within the signed range. Otherwise, signed overflow occurs.
Frequently asked questions
1. How do I subtract binary numbers?
Align the digits from right to left and subtract one bit at a time. When subtracting 1 from 0, borrow from a more significant bit. You can also use the two's-complement method: invert the subtrahend, add 1, and add it to the minuend.
2. What happens when the result is negative?
The exact mathematical result is negative when the subtrahend is larger than the minuend. A fixed-width unsigned result wraps modulo 2n. The same bits may represent a negative number when interpreted as two's-complement signed arithmetic.
3. What is the difference between unsigned and signed results?
Unsigned interpretation treats every bit as part of a non-negative number. Signed two's-complement interpretation treats the most significant bit as the sign-weighted bit. For n bits, the signed range is −2n−1 through 2n−1 − 1.
4. What does borrow or unsigned underflow mean?
In unsigned subtraction, a final borrow indicates that A is smaller than B. The fixed-width bit result is still calculated, but it is not the ordinary non-negative mathematical difference unless the operands were in range with A at least B.
5. What is signed overflow in binary subtraction?
Signed overflow occurs when the exact mathematical result lies outside the range representable by the selected two's-complement width. The resulting bits still exist, but interpreting them as a signed integer does not give the exact mathematical result.
6. Can I subtract binary numbers with different lengths?
Yes. The calculator pads the shorter operand with leading zeros to the selected width. Both operands must fit within that width.
7. Does this calculator send my input to a server?
No. The subtraction is performed locally in your browser using JavaScript. PHP is used to serve the page, not to process the calculation form.