29
$\begingroup$

About to read computer science, I have just stumbled accross the concept of "Two's complement". I understand how to apply the "algorithm" to calculate these on paper, but I have not yet obtained an understanding of why it works. I think this site: https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html provides an explanaition why "flipping the digits" and adding one produces the compliment. What I do not understand is why adding the complement is equivalent to substracting the original number. Could somebody please give an explanation (maybe with a decimal example of the same concept as well?)?

Many thanks!

$\endgroup$
2
  • 2
    $\begingroup$ For three bits the decimal number 3 is 011 and the two's compliment is 101. When you add 011 and 101 you get 1000, but the fourth bit is shifted out of a three bit register, so to three bits the sum is 000. en.wikipedia.org/wiki/Two%27s_complement $\endgroup$
    – MaxW
    Commented Sep 9, 2016 at 19:42
  • $\begingroup$ before you add the 1 you get $11001101 \rightarrow 00110010$ and $11001101 + 00110010 = 11111111$. So when you do add the 1, you get $11001101 + 00110011 = 00000000$. So $00110011 = -11001101$. So $x = - twoscomplement$. $\endgroup$
    – fleablood
    Commented Sep 9, 2016 at 20:34

5 Answers 5

70
$\begingroup$

I'll stick to 8-bit quantities, but the same applies in general.

The key to understanding two's complement is to note that we have a set of finitely many (in particular, $2^8$) values in which there is a sensible notion of addition by $1$ that allows us to cycle through all of the numbers. In particular, we have a system of modular arithmetic, in this case modulo $2^8 = 256$.


Intuitively, arithmetic modulo $n$ is a system of addition (and subtraction) in which overflow and underflow cause you to "cycle back" to a value from $0$ to $n-1$. A classic example is the usual "clock arithmetic", which is to say arithmetic modulo $12$.

For example, if it is $11\!:\!00$ now, then three hours later it will be $2\!:\!00$, since $$ 11 + 3 = 14 \equiv 2 \pmod {12} $$ and similarly, if it is $1\!:\!00$, then $4$ hours ago it was $9$ since $$ 1 - 4 = -3 \equiv 9 \pmod{12} $$ Notice that subtracting $4$ hours on the clock is the same as adding $12 - 4 = 8$ hours. In particular, we could have computed the above as follows: $$ 1 - 4 \equiv 1 + 8 = 9 \pmod{12} $$ That is: when performing arithmetic modulo $n$, we can subtract $x$ by adding $n-x$.


Now, let's apply this idea modulo $256$. How do you subtract $3$? Well, by the above logic, this is the same as adding $256 - 3 = 253$. In binary notation, we could say that subtracting $00000011$ is the same as adding $$ 1\overbrace{00000000}^8 - 00000011 = 1 + \overbrace{11111111}^8 - 00000011 = 11111101 $$ and there's your two's complement: the calculation $(11111111 - 00000011)$ "flips the bits" of $00000011$, and we add $1$ to this result.


Note 1: In the context of arithmetic with signed integers, we don't think of $11111101$ as being $253$ in our $8$-bit system, we instead consider it to represent the number $-3$. Rather than having our numbers go from $0$ to $255$ around a clock, we have them go from $-128$ to $127$, where $-x$ occupies the same spot that $n - x$ would occupy for values of $x$ from $1$ to $128$.

Succinctly, this amounts to saying that a number with 8 binary digits is deemed negative if and only if its leading digit (its "most significant" digit) is a $1$. For this reason, the leading digit is referred to as the "sign bit" in this context.

Note 2: An interesting infinite analog to the two's complement system of subtraction is that of infinite series 2-adic numbers. In particular, we can say something strange like $$ \dots 11111 = -1 $$ since $\dots 11111$ is the "infinite two's complement" of $1$.

$\endgroup$
4
  • 4
    $\begingroup$ I've seen a lot of explanations of the matter and this is the best ever. $\endgroup$
    – super.t
    Commented Dec 20, 2021 at 21:30
  • $\begingroup$ Nice explanation! I am just wondering how 1 + 11111111 - 00000011 can lead to flip and sum 1 since subtraction is not associative. I understand but it's not that clear. If you could share some thoughts about the associativeness it would be great. $\endgroup$
    – GeekSilva
    Commented Dec 27, 2023 at 6:54
  • 1
    $\begingroup$ @GeekSilva I'm not sure where exactly you think the non-associativity of subtraction fits in here. It is generally true that $$ a + (b - c) = (a + b) - c. $$ You can think of this as a consequence of the associativity of addition, since $$ a + (b + (-c)) = (a + b) + (-c). $$ $\endgroup$ Commented Dec 27, 2023 at 14:15
  • $\begingroup$ Thanks a lot, @BenGrossmann. Honestly, I was just being silly - and performing operations incorrectly most of the time. I realized how it works and your comment clarified more. $\endgroup$
    – GeekSilva
    Commented Dec 28, 2023 at 1:47
6
$\begingroup$

Let's look at a decimal example. You want to do $735-78$.

Borrow 1000 from the Number Bank; the loan is subject to no interest, but you must give back what you got as soon as you have used it.

Now consider that $$ 735-78=735+(1000-78)-1000 $$ The subtraction $1000-78$ is very easy to do: just do $9$-complement on the rightmost three digits (the missing one at the far left is, of course, $0$), getting $921+1$, so our operation now reads $$ 735-78=735+921+1-1000 $$ Since \begin{array}{rr} 735 & + \\ 921 & = \\ \hline 1656 \end{array} we can give back 1000 to the bank and add 1: $$ 735-78=656+1=657 $$

In base two it's exactly the same, with the only difference that $1$-complement (instead of $9$-complement) is very easy, because it consists in flipping the digits. You don't need the loan either, because you work on a fixed number of bits, and numbers that overflow are simply reduced forgetting the leftmost digit. So if you have to do

00101001 - 00001110

you can flip the digits in the second number and add, forgetting the leftmost bit that may become 1:

00101001 +
11110001 =
----------
00011010 +
       1 =
----------
00011011
$\endgroup$
2
  • $\begingroup$ What is the reason for splitting off the $+1$ in the second equation? $\endgroup$
    – Jacob Bond
    Commented Sep 9, 2016 at 20:10
  • 1
    $\begingroup$ @JacobBond When you do $9$-complement you are off by $1$ for the rightmost digit. $\endgroup$
    – egreg
    Commented Sep 9, 2016 at 20:12
1
$\begingroup$

Basic assumption is that we are doing modulo arithmetic over some $2^{n+1}$. i.e. $x \equiv x + 2^{n+1}$ for all $x$. (I suppose I should write $x \equiv x + 2^{n+1} \pmod{2^{n+1}}$ but I'm not going to write the $(\operatorname{mod}{2^{n+1}})$ part.) Especially $0 \equiv 2^{n+1}$.

So what we want to show is the $-x \equiv 2^{n+1} - x$ is "twos complement". If $x = \sum_{i=0}^{n} a_i 2^i; a_i = \{0,1\}$, the "twos complement" is $\overline x = (\sum_{i=0}^{n} b_i 2^i) + 1$ where $a_i + b_i = 1$. So $$\begin{align} x + \overline x &= ( \sum_{i=0}^{n} a_i 2^i) + (\sum_{i=0}^{n} b_i 2^i) + 1 \\ &= (\sum_{i=0}^{n}(a_i + b_i)*2^i) + 1 \\ &= (\sum_{i=0}^{n} 1*2^i) +1 \\ &= 11\ldots11_2 + 1 &&\text{(subscript $2$ denotes binary)} \\ &\equiv 00\ldots00_2 \\ &\equiv (2^{n+1} - 1) + 1 \\ &= 2^{n+1} \\ &\equiv 0 \end{align}$$

So $\overline x = 2^{n+1} -x \equiv 0 - x = -x$

Example: Let $n = 8$ so we have numbers $0, \ldots, 255$ with $256 \equiv 0$, set $x = 100 = 64 + 32 + 4 = 01100100_2$ and $y = 10011011_2 = 155$. Then $$x + y = 11111111_2 = 255 = 256 - 1 \equiv -1$$ and $$z = y + 1 = 10011011_2 + 1 = 10011100_2 = 156$$ However $z = y+ 1 = (x+y) +1 - x = (256 -1) +1 - x = 256 -x \equiv -x \equiv -100$.

And if we actually do it: $y = 10011011_2=128 + 16 + 8 + 2 + 1 = 155$

$z = 10011100_2 = 128 + 16 + 8 + 4 = 156$.

And, indeed, $156 + 100 = 256 \equiv 0$ so $156 \equiv -100 \pmod{256}$.


Another way to think of it is if we were to do "10s compliment:

To find $-15,890,256 \bmod 1{,}000{,}000{,}000$ we would do "10 compliment" of $015,890,256$ which has decimal expansion $$(9-0)(9-1)(9-5)(9-8)(9-9)(9-0)(9-2)(9-5)(9-6) = 984{,}109{,}743.$$ [Note: $984{,}109{,}743 + 15{,}890{,}256=999{,}999{,}999$.]

Add $1$ to get $984{,}109{,}744$ and, indeed $984,109,744 \equiv -15,890,256 \pmod {1{,}000{,}000{,}000}$.

But "$2$" only has two values, $0$ and $1$, so it is easier.

$\endgroup$
2
  • 3
    $\begingroup$ Gratuitous profanity. And unreadable answer anyway. $\endgroup$
    – egreg
    Commented Sep 9, 2016 at 20:17
  • $\begingroup$ I was actually hoping to see this process generalized to any number, thanks for such a thorough explanation. $\endgroup$ Commented Mar 24, 2019 at 20:25
1
$\begingroup$

The way we represent it is ambiguous. If you are explicit about all unstated bits, then simply flipping all bits negates the number. But you need to have a representation with a decimal point and bits stating what all unstated bits are. For example, say that we are explicit about what unstated bits are so that we can combine signed, unsigned, and uncarried numbers explicitly:

This is zero: 0..0000.0000..0

Use "..1" to represent that there are no zeroes on the right: 0..0000.1111..1

If you perform the carry, you get zeroes on the right and it becomes 1. This is a mechanical definition that can be checked and done in an actual (finite) machine. Then -1 is:

1..1111.0000..0

Add 1 + -1:

0..0000.1111..1 + 1.1111.0000..0

That gives you: 1..1111.1111..1, which has a carry to be done due to the "..1". So after carry, it's 0..0000.0000..0.

So now we negate an integer. It has all zero bits on the right. So it triggers a carry, which is the same as adding 1.

0..010.000..0

1..101.111..1

1..110.000..0

So the fact that you "flip the bits and add 1" is a little bit of an illusion created by being ambiguous about what all the bits are. It is an emergent optimization for finite integer representations to state it that way.

Negate 1/2:

0..000.100..0

1..111.011..1

1..111.100..0

That's -1 + 1/2.

$\endgroup$
0
$\begingroup$

With the help of the other answers on this post (especially Ben Grossmann's), I managed to figure out Two's Complement and why it works for myself, but I wanted to add another complete barebones answer for anyone who still can't understand. This is my first post, so thank you for reading in advance. Also, much of my mathematic notation is likely to be false, so please refer to other answers for more mathematically accurate explanations.

As Ben Grossmann pointed out, understanding that binary addition is modulo is the key to understanding how Two's Complement works. What that means in a binary sense is that the last carry doesn't get used, so: 1111 1111 + 1 = 0000 0000, not 1 0000 0000.

In decimal, this looks like $(255+1)\bmod{2^8}$. A similar example that you may find easier to wrap your head around is $(a+b)\bmod{12}$, which should look familiar.

This works for addition, but how about modulo subtraction? Well, continuing with the clock example, if we want to subtract using modulo addition, there is an easy solution: $a+(12-b) \pmod{12}$, or in binary: $a+(2^8-b)\bmod{2^8}$. The $12$ and $2^8$ are canceled out by their corresponding modulo, leaving us with $a-b$. The trick is now getting $2^8-b$, and that trick lies in Two's Complement.

To derive $2^8-b$, or $1\ 0000\ 0000 - b$ using only 8 bits, we first have to convert that into a familiar format:

$1\ 0000\ 0000-b=1111\ 1111-b+1$

This is the equivalent of $11-b+1$ using 12 as our modulo. Subtracting a number from $1111\ 1111$ is the same as inverting it. If this is confusing, then consider the equation

$1111\ 1111-1101\ 1001$

As you may have noticed, no borrows occur, because there are no $0$s in $2^8-1$. This effectively means that every bit of b is inverted. And so $INV(b)$ can be substituted for $1111\ 1111 - b$.

Plug that into our previous equation, and we now have $a-b=a+INV(b)+1$. There you have Two's Complement.

What this means for negative numbers is that $1111\ 1111=-1$ (as explained above in more detail by Ben Grossmann) and $1000\ 0000=-128$, while $0000\ 0000=0$ and $0111\ 1111=127$. The system cycles through first the positives, and once 128 is reached ($1000\ 0000$) it flips the sign and cycles the other way through the negatives. The seventh bit acts like that sign, and no actual sign flip is needed. Without any additional provisions needing to be made, we can now add both negative and positive numbers.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .