How to get rid of subtraction

Abstract

What's this 2-complement thing, anyway?

With (signed) integers, \(a-b\) is the same as \(a+(-b)\), suggesting every subtraction can be replaced by an addition. But we were not taught to use that for practical calculations: it doesn't even seem to be possible to compute eg \(123+(-42)\) other than by subtracting 42 from 123. However, computers use binary arithmetic and actually perform subtraction by adding the "2-complement". How does this work? What's the 2-complement, exactly? Could we use something similar for our everyday, decimal, calculations?

Note: this is a written version of a talk I gave on september 9th 2025 on the "What the Cup?!" lighthearted seminar that takes place alongside the coffee break of the LCIS lab. Here are the original slides.

image.png


Arithmetic operations

Addition and subtraction on natural numbers

When we were kids, we learned to write numbers using the decimal positional numbering system. A numbering system is a scheme to write numbers, it is decimal because we use ten different digits and positional because the value each digit stands for in the number's representation depends on its position in the representation.

We're so familiar with this system that when we see eg 123 we don't read this as the three digits "one two three" but as the number "one hundred twenty three", acknowledging that its value is defined as

\[ 123 = 1×10^2 + 2×10^1 + 3×10^0 \]

This system is very nice. First it's rather compact (each added digit allows for ten times more numbers), which is convenient, but the key point to its success is that it allows to use efficient algorithms for computing arithmetic operations.

As kids, we have learnt to perform an addition by writing the two numbers to be added one on top of the other, and adding the digits from each number from right to left. On the example addition below, we first add 3 and 9, which is 12, so we write 2 in the result line and keep 1 as a carry (not shown on the example). We then add 2, 4 and the carry, and get 7. Finally we add 1 to the implicit 0 in front of 49.

        123           172
      +  49         -  49
      -----         -----
        172           123

On the right is an example of subtraction, which is very similar, only in reverse. We first need to substract 9 from 2, which we can't, so we borrow 1 from the 7 on the left and compute 12-9 which is 3. Then we perform 6 (7 minus the 1 we have just borrowed) - 4 which is 2, and finally 1 - (implicit) 0.

Such algorithms have been introduced in Europe by a book from the Persian mathematician Al-Kwarizmi (whose portrait is on the picture above). The word "algorithm" actually comes from its name!

Introducing (signed) integers

There's an annoying issue with the subtraction operation: contrary to addition which is total on the natural numbers (whatever pair of natural numbers \(a\) and \(b\) we choose, their sum \(a+b\) is always defined), subtraction is not. Obviously if \(b\) is larger than \(a\), \(a-b\) is not a natural number. Since natural numbers were designed to express quantity, allowing such a value to be defined amounts to creating the concept of missing, or "negative" quantity.

And so natural numbers were extended to (signed) integers1. For any positive integer \(a\), we define its opposite (or additive inverse or negation) \(a'\) as the number such that \(a + a' = 0\). We also define the arithmetic negation operation to produce the opposite of a number. It's denoted by the minus symbol so that the opposite of \(a\) is noted \(-a\). There is a bit of overloading on the notation, since now the \(-\) operator denotes both subtraction and opposite, plus is also used as the sign in the representation of negative numbers, such as -123. In practice it's not an issue since it's very easy to decide from the context what any occurence of \(-\) stands for2.

Now that we have both positive and negative numbers and the opposite operation, there's an interesting algebraic property:

\[ a - b = a + (-b) \]

This suggests that subtraction is now just a specific case of addition.

Theoretically, this is very nice, but unfortunately this seems to have little practical consequence: it doesn't seem to be usefull for numeric calculations. Suppose I ask you to compute \(43-12\), you're not going to think "hey that's a subtraction, I don't like that, so let's express it as an addition, and compute \(43+(-12)\) instead". More than that, how would you compute the following addition: \(34+(-8)\)?

So it seems that for numerical calculations we can't actually get rid of subtraction by replacing it with addition. Even worse, now that we have negative numbers, we actually need to use the subtraction algorithm for some additions!

We all know the rules, but let me recall them anyway: to compute a+b in \(\mathbb{Z}\) there are four different cases to consider:

\( \newcommand{\abs}[1]{\lvert #1 \rvert} \)

\begin{matrix} a>0 & b>0 & & a+b \\ a>0 & b<0 & a\ge\abs{b} & a-\abs{b} \\ a>0 & b<0 & a\le\abs{b} & -(\abs{b}-a) \\ a<0 & b<0 & & -(\abs{a}+\abs{b})\\ \end{matrix}

Now, I'm not claiming that this is difficult (it's certainly not), but it seems a bit cumbersome and and it sounds quite unsatisfying to me.

Computer arithmetic

Something that had bothered me for a long time was that I had the impression (but absolutely not a clear understanding), that somehow, computers actually manage to handle this in a more straightforward way. Below is a schematic view of an add/subtract circuit as implemented in a CPU. It clearly suggests that computers actually manage to only perform additions. So maybe there's a practical way to actually use the \(a-b=a+(-b)\) formula for numerical calculations?

addsub.png
Schematic view of an add/subtract circuit

Computers must have a different way than us to perform numerical calculations, right? There are actually two ways in which computer arithmetic differs from everyday arithmetic. The first difference is well known by everyone. Even people who know very little about computers somewhat "know" that computers use binary numbers. To be precise, in a computer, numbers are represented in the binary positional numbering system, which is exactly as the decimal positional numbering system except, well, it's binary instead of decimal. So instead of using ten digits 0, 1, … 9, we only have two digits 0 and 1.

Again, it may look quite different on the surface but it is essentially the same system. Each digit in the number represents a power of 2 value (instead of a power of 10): \(1010 = 1×2^3 + 0×2^2 + 1×2^1 + 0×2^0\) (which is \(8+2=10\), in decimal).

Since this is essentially the same system, the same algorithms can be used to perform arithmetic operations:

  1010
 +  10
 -----
  1100

Just as in decimal, we have to proceed from right to left:

  • 0+0 is 0, so we write the rightmost digit of the result
  • 1+1 is 10 (in binary) so the result is 0 and we carry the 1 to the operation on the left
  • 0+0+1 (the carry) is 1
  • 1+0 is 1

Please note that there are actually two results for each bit: the result proper and the output carry. Consequently, each "digit addition" (except the rightmost one) actually gets three digits to add: the digits from the two numbers and the input carry coming from the operation on its right.

Of course the same goes for subtraction. I won't go through an example, since there's really nothing more. Additionally, as we'll see, computers don't actually use the subtraction algorithm.

Now, binary numbers are quite longer (I should rather say the binary representation of numbers is longer, but I think you get what I mean) and thus manually applying the algorithms will quickly become quite cumbersome, but this is only because we humans are not very good at performing long sequences of simple, repetitive operations.

Performing a binary addition in hardware

So we can use Al-Kwarizmi's algorithm to add binary numbers. Let's now see how the algorithm can be performed by hardware. As seen above, to perform a binary addition, we need to execute a sequence of "digit additions". Here's a description of a "one bit adder" circuit. It has three inputs (one bit from each of the two numbers, plus the input carry) and two outputs (the result and the output carry). I won't show how it can actually be done but it's relatively easy to build such a circuit with a few logic gates.

1bitadder.png
1-bit adder

Once we have this, we just have to combine as many one bit adders as there are digits in the numbers we want to add. Here's a 4-bit adder circuit:

4bitadder.png
4-bit adder

Now, there's no reason we couldn't build a 1-bit subtracter and from that a 4-bit subtraction circuit, it would be very similar. But as already suggested, this is not the way things are implemented in CPUs.

What about subtraction? The two-complement

On the schematic view above, the [-] box actually computes the two-complement. We may want to think of it as the opposite, since to subtract b from a, the circuit adds b's two-complement to a, but just saying that doesn't quite bring any hindsight. I mean what does it mean, concretly, to "take the opposite"? Moreover, as you may know, CPUs can actually manipulate two kinds of (integer) numbers:

  • signed ints, which are "roughly like" mathematic integers
  • unsigned ints, which are "roughly like" natural numbers

(more on what I mean by "roughly like" soon). If we consider the case of unsigned ints, it just makes no sense to understand "add b's two-complement to a" as "add b's opposite to a".

I said above that are two ways in which computer arithmetic differs, and the second one, which is actually key to this issue, is that it's finite arithmetic. For example, in a 4-bit computer, only 4-bit numbers can be manipulated, and the result of adding (or subtracting, or whatever) two 4-bit numbers must also be a 4-bit number. Assume we add binary numbers 1010 and 1000:

   1010
  +1000
  -----
(1)0100

The result would need 5 bits but since all numbers are stored on 4 bits, it actually is 0100. At least the circuit signals when the result would actually need to be expressed on 5 bits, with the leftmost output carry (labelled ov for overflow in the above picture).

Finite arithmetic has a property that at first sounds very odd. In the case of binary arithmetic with n-bit numbers this property is

\( \forall a, a+2^n = a \)

This is what I meant by "roughly like" above! Personally, until I understood this, I had always considered the fact that computer arithmetic is finite to be some "unavoidable defect": you're fine if the numbers you manipulate are small enough, otherwise there's an overflow and the result is incorrect (according to "infinite arithmetic"). If you really want to get correct results on larger numbers, you can always store numbers on several memory words, chain operations and check for the overflow3.

However, this can also be seen as a feature, as we can write:

\( a - b = a - b + 2^n = a + \textcolor{blue}{(2^n - b)} \)

in which we have replaced a subtraction with an addition.

Wait, isn't \(2^n-b\) a subtraction?

I can hear you: now we have an addition, for sure, but we still have a subtraction as well! Well, yes, but actually no: at least this is not an arbitrary subtraction. Whatever numbers a and b I consider, the subtraction will always be \(2^n - b\), so this is a very special case. Could it be that we can compute this without the subtraction algorithm?

Let's see how it's done in a computer chip. Here's what's inside the add/sub circuit in a 4-bit computer:

4bitaddsub.png
A 4-bit add/subtract circuit

As inputs, we have the four bits \(a_3, ... a_0\) and \(b_3, ... b_0\) of the two input numbers and an "operation selection signal". 0 asks for an addition and 1 for a subtraction.

The curvy gates at the top are "xor" (for exclusive or) gates. They implement the exclusive or logical operation whose result is one if and only if one of its two inputs (but not both) is equal to 1. Its truth table is

a b a xor b
0 0 0
0 1 1
1 0 1
1 1 0

Here the second input of the gates is the +/- selection signal. If we ask for an addition, its value is 0, and the output of the gate is the value of the first input (a xor 0 = a, whatever a).

In this case we simply add the bits of a with the bits of b. Also note that the input carry of the rightmost 1-bit adder is set to 0

  • add: \( a_3 a_2 a_1 a_0 + b_3 b_2 b_1 b_0 + 0 \)

If we ask for a subtraction then the output of the gate is the logical complement of the first input, so the 1-bit adders get the negation of the b bits. Also, the input carry of the rightmost 1-bit adder is set to 1.

  • sub: \( a_3 a_2 a_1 a_0 + \overline{b_3} \overline{b_2} \overline{b_1} \overline{b_0} + 1 \)

Computing the two-complement

So in the case of a subtraction we want to compute \( a + (2^n-b) \), and it seems what the chip does compute is \(a + (\overline{b}+1)\). If we want to match these, we can write it as \( a + (2^n-b) = a + (\textcolor{blue}{2^n-1-b}+1) \).

It turns out that \( 2^n-1-b \) has a name, it's called the one-complement of \(b\).

 2-c                1-c
    10000  (2^4)        1111  (2^4-1)
   - bbbb             - bbbb
   ------             ------

It's easy to see that for computing the one-complement we don't actually need the full generality of the subtraction algorithm. Each digit operation is either 1-1 and the result is 0, or 1-0 and the result is 1, so in the end the result is just the inversion of the b bit. Also, since no borrow can occur there's no constrain on the order in which the bits are to be processed. And actually, on the circuit above, all the bits are processed by the xor gates in parallel, simultaneously!

Computing the one-complement is incredibly simple, but to get the two-complement we have to add 1 to it, so we end up with two additions, right? We could argue the second one is quite simple since, well, it's just adding 1, but it's not actually simpler, I mean you could still have a carry to propagate through the full chain of 1-bit adders. However, as you saw on the figure, we actually get this second addition for free, since we can simply use the input carry of the rightmost 1-bit adder!

What do you think the first engineer that realized this could be done this way thought? Probably something like "Hey, this is great!". Indeed, very clever design if you ask me!

Can we do that in the decimal system?

Now, back to human pen and paper calculations. Is this something that is made possible by the fact there are only two digits in binary, or can something similar be done in decimal ?

Let's try to transpose that to the decimal system. If we manipulate numbers with n digits, we can define the 10-complement and express the base 10 version of our "interesting property" of finite arithmetic:

\[ a - b = a + 10^n - b \]

Let's try to apply this to the calculation of \(382-247\). We first need to choose the value of n, here we'll obviously take n=3, so the 10-complement of b is \(1000 - b\). We can compute it by performing a subtraction, as:

   1000
 -  247
 ------
    753

But our point was to avoid using the subtraction algorithm! Remember that in binary, we manage to compute the 2-complement by adding one to the 1-complement. Analogously we can compute the 10-complement by adding one to the 9-complement. The 9-complement of b is simply \(10^3-1 - b\) , ie \(999 - b\):

    999
  - 247
  -----
    752

Again, we don't need the subtraction algorithm for this! First, no borrow can occur, so we can process the digits in the order we please. Second, we only need to be able to compute the difference between 9 and an arbitrary digit: \(9-0=9, 9-1=8, ... 9-9=0\).

Now we add 1 to get the 10-complement of b, and add it to a:

   382
 + 753
 -----
(1)135

To summarize, here're the steps to compute a-b without using the subtraction algorithm:

  1. choose n
  2. compute 9-complement of b
  3. add 1 to get 10-complement of b
  4. add a to 10-complement of b (throwing away excess digit)

The algorithms involved are the addition and the 9-complement computation (which, again, is not a subtraction).

So have we succeeded in getting rid of subtraction? Let "double-u" tell us!

mission-accomplished.jpg

Footnotes


1

In french they are called entiers relatifs.

2

This may be mildly annoying for students building a parser in a compilation course project…

3

Nowadays, lots of programming languages provide for "arbitrarily large" integers (eg Python's int type) and that's exactly what they do.