$ man binary-subtraction-calculator
Binary Subtraction Calculator
NAME
binary-subtraction-calculator — Subtract binary numbers with the borrow row shown, plus two's and ones' complement working, bit width, and signed overflow versus unsigned underflow.
SYNOPSIS
# What is a binary subtraction calculator?
A binary subtraction calculator subtracts one binary number from another and shows the working three ways — column subtraction with borrows, two's complement, and ones' complement with end-around carry — with the decimal and hexadecimal check alongside.
WHY HERE
Number-base and data utilities for developers working with APIs.
1 1 borrows 0 1 1 0 1 - 0 0 1 1 0 ───────── 0 0 1 1 1
0 1 1 0 1 A + 1 1 0 1 0 two's complement of B (invert, add 1) ───────── 1 0 0 1 1 1 carry out discarded
0 1 1 0 1 A + 1 1 0 0 1 ones' complement of B (invert only) ───────── 1 0 0 1 1 0 + 0 0 0 0 1 end-around carry ───────── 0 0 1 1 1
- In decimal the sum is 13 − 6 = 7, which is 111 in binary.
- Borrow method: subtract column by column from the right; where the top bit is smaller, borrow 1 from the next column, worth 2 here. Result 00111.
- Two's complement: invert B to 11001, add 1 to get 11010, then add to A. The sum is 100111; discard the carry out, leaving 00111.
- Ones' complement: invert B to 11001 and add. The carry out is added back into the low bit (end-around carry), giving 00111.
A borrow in binary is worth 2 in the column receiving it, not 10 — that is the single place decimal habits mislead. The carry discarded in the two's complement method is the 2^5 that the complement representation added in; dropping it is part of the method, not a rounding error.
Whole binary numbers only, up to 64 bits, entered as unsigned bit patterns. The signed readings assume two's complement — essentially universal in modern hardware, but not the only convention, which is why all three methods are shown. Nothing you type is stored or sent anywhere.
// Whole binary numbers only, up to 64 bits. Binary fractions are not accepted: the borrow method extends to them but the complement methods are defined on fixed-width integers, so a binary point has nowhere to go. Operands are taken as unsigned bit patterns, without a minus sign — the sign of the answer is what the tool works out. The signed readings assume two's complement, which is what essentially all modern hardware uses but is not the only convention; sign-magnitude and ones' complement machines read the same bits differently, which is the point of showing all three. Nothing you type is stored or sent anywhere.
USAGE
01 → Enter both numbers in binary
Ones and zeros only — spaces and underscores are ignored so 1101 0110 and 1101_0110 both work. A stray digit is named and refused rather than silently truncated, which is what parseInt would do to you.
02 → Set the bit width, or leave it on auto
Auto picks the narrowest width that cannot overflow. Set it narrower on purpose if you want to see overflow happen — that is the most instructive thing this tool does, and a width of four is where most textbook examples live.
03 → Read the borrow row, not just the answer
The borrow row sits above the digits exactly as you would write it on paper. A borrow in binary is worth 2 in the column it lands in, not 10 — that is the one place decimal habits mislead.
04 → Check the signed and unsigned verdicts separately
The same bit pattern means two different numbers. In four bits, 1101 − 0110 is a perfectly good unsigned 13 − 6 = 7, and simultaneously a signed overflow, because −3 − 6 = −9 does not fit in −8 to 7. Both are reported because both are true.
DESCRIPTION
Column subtraction in binary works exactly like decimal long subtraction, with one difference that trips everybody: a borrow is worth 2, not 10. Take 1101 − 0110. The rightmost column is 1 − 0 = 1. The next is 0 − 1, so borrow from the left: 10₂ − 1 = 1, and the borrow propagates. The third column is now 1 − 1 − 1 = −1, so borrow again: 1. The leftmost is 1 − 0 − 1 = 0. The answer is 0111, which is 7, and 13 − 6 = 7 checks out. The borrow row for that sum reads 1 1 0 0 from the left.
Hardware does not do that. A processor has an adder, and building a separate subtractor would double the silicon for no reason, so subtraction is turned into addition: A − B = A + (−B), with −B formed in two's complement by inverting every bit and adding one. For the same sum in five bits, B = 00110 inverts to 11001, plus one is 11010, and 01101 + 11010 = 100111. Throw away the carry out of the top bit and 00111 is left — 7 again. The carry you discard is not a rounding error; it is the 2ⁿ that the complement representation added in and that the discard takes back out.
Two things can go wrong, and they are different things. If there is no carry out of that addition, the minuend was the smaller number and the unsigned result has wrapped — an unsigned underflow. Separately, reading the same bits as signed two's complement, subtraction overflows when the operands have different signs and the result takes the wrong sign: 1101 − 0110 in four bits is −3 − 6 = −9, which is outside the four-bit signed range of −8 to 7, so the 0111 it produces is a correct unsigned answer and a wrong signed one. A processor sets the carry flag for the first condition and the overflow flag for the second, and confusing them is the classic bug.
Ones' complement is the same trick without the add-one: invert B, add, and if a carry falls out of the top, add it back into the bottom bit. That end-around carry is what compensates for the missing one. It works, and it was used in real machines — the CDC 6600, the PDP-1 — but it has two representations of zero, 0000 and 1111, so every comparison has to check for both. Two's complement has one zero and no end-around carry, which is why it won and why your compiler has never mentioned ones' complement to you.
FAQ
## How do you subtract binary numbers?
Column by column from the right, borrowing from the next column when the top bit is smaller. A borrow is worth 2 in the column receiving it. So 1101 − 0110 = 0111, which is 13 − 6 = 7 in decimal.
## How does two's complement subtraction work?
Invert every bit of the subtrahend, add one to get its negative, add that to the minuend, and discard any carry out of the top bit. The discarded carry is the 2ⁿ the representation borrowed; dropping it is part of the method, not an error.
## What is the difference between signed overflow and unsigned underflow?
Unsigned underflow means there was no carry out of the addition, so the minuend was smaller and the unsigned answer wrapped. Signed overflow means the operands had different signs and the result took the wrong sign, so it fell outside the representable signed range. A processor flags them with separate bits — carry and overflow — because they are independent.
## What is an end-around carry?
In ones' complement arithmetic, a carry out of the most significant bit is added back into the least significant bit. It makes up for the fact that ones' complement negation inverts without adding one. Two's complement needs no such step, which is one reason it replaced it.
## What happens if the second number is bigger?
The true answer is negative. The borrow method has no way to express that, so the tool swaps the operands, subtracts, negates the result and tells you it did. The complement methods handle it natively: the result comes out as a negative value in the chosen width, provided the width is wide enough to hold it.