When I first learned how a computer handles negative numbers and subtraction, I found the mechanism unexpectedly compelling.

At the hardware level, building a circuit that adds two binary numbers is relatively straightforward. Implementing subtraction as a separate operation, however, introduces additional complexity.

Early computer architects faced an obvious question: how should negative numbers be represented in binary so that the hardware remains as simple as possible?

The most intuitive approach is called sign-and-magnitude. In a four-bit system, if the first bit represents the sign ( for positive, for negative) and the remaining three bits represent the value, then is and is .

For a human reader, this is clean and explicit. For an adder circuit, however, it fails.

If you pass () and () into an ordinary binary adder, the circuit simply adds the bits together and produces with a carry-out of . Interpreted as sign-and-magnitude, the four-bit result is , not the expected . Worse, the system now has two different representations for zero ( and ), and the hardware must inspect the signs and magnitudes before deciding whether to add or subtract.

There is a slightly different approach. Ones’ complement is a binary numeral system where the negative form of an -bit integer is obtained by subtracting the absolute value of the integer from .

In a four-bit system, is represented as . To represent using ones’ complement:

   1111 (2⁴ - 1 = 15)
-  0011 (3, the absolute value of -3)
-------
   1100

Or in practice, you invert every bit to form a negative number, turning () into (). This brings the arithmetic closer to working, but it still leaves two zeros ( and ) and requires an extra step, an operation called “end-around carry,” where any overflow bit must be wrapped around and added back to the lowest bit.

End-around carry is an additional requirement imposed by the representation.

In ones’ complement, the bit pattern representing a negative integer has the unsigned value , where .

Then, (where and are integers and ).

Here, represents a carry-out beyond the -th bit, and discarding that leaves , which is off by exactly 1.

Adding that discarded carry back into the lowest position addresses the issue:

There is a reason for that missing one.

In ones’ complement, zero has two distinct representations: positive zero (), which is and negative zero (), which is .

Consider the four-bit patterns for the values around zero, arranged in unsigned binary order:

Value       Bit Pattern (4-bit)
-------------------------------
-1          1110
-0          1111
+0          0000
+1          0001

The extra representation of zero reveals something important about ones’ complement. Its arithmetic effectively wraps around modulo rather than modulo . When an addition produces a carry beyond the fixed bit width, simply discarding it would mean dropping , which is equivalent to modulo .

In other words, when subtracting a smaller positive integer from a larger one using ones’ complement, discarding the carry-out leaves a result that is one too small. The end-around carry corrects this by adding the discarded bit back to the least significant bit.

However, when subtracting a larger positive integer from a smaller one using ones’ complement, no end-around carry occurs.

(where and are integers and ).

An end-around carry occurs the result generates an overflow out of the -th bit, meaning: .

Because and both are integers, .

So .

Since the result is strictly smaller than , no carry-out of the most significant bit is ever produced.

When , no carry-out occurs, and the result is , which is negative zero ( in a four-bit system).

What modern computers actually use is two’s complement. Two’s complement is a representation of signed integers where the negative of an -bit integer is represented by subtracting the absolute value of the integer from .

In a four-bit system, is represented as . To represent using two’s complement:

   10000 (2⁴ = 16)
-   0011 (3, the absolute value of -3)
--------
    1101

Or in practice, you invert all the bits and add . In a four-bit system, is . Inverting the bits gives , and adding gives .

At first glance, looks like an arbitrary way to write . Looking at the bits alone, the symmetry of sign-and-magnitude is gone.

What replaces it is structural convenience.

In a four-bit two’s complement system, the leftmost bit acts as a negative weight (, or ), while the remaining bits remain positive (, , ). So is not “a negative sign followed by 5.” It is , which equals .

Zero now has only one representation: . If you invert it () and add , you get . In a four-bit register, the fifth bit overflows and is discarded, leaving again.

The real advantage appears when the computer needs to subtract. Instead of subtracting from , the computer adds to :

  0101 (5)
+ 1101 (-3)
------
 10010

Because the register only holds four bits, the leading overflows the four-bit boundary and is simply dropped.

What remains in the register is , which is .

The adder did not need to know that it was subtracting. It did not need to compare the magnitudes or treat negative operands differently. It simply performed the same binary addition, discarded the carry beyond the fixed bit width, and produced the correct result.

What I found compelling was the practicality of that decision.

The system was designed by humans, but its internal representation did not need to follow human intuition. Sign-and-magnitude would have made negative numbers easier to read, but that was not what the hardware needed. Two’s complement gave up that human-friendly representation in exchange for simpler arithmetic.

After all, I do not need a computer to represent negative numbers in a way that makes immediate sense to me. I need the result to be correct.