All modern computers store numbers in memory in binary format. For representing negative integers, they use either more simple and less common one's complement notation obtained by inverting all digits in a number, or more common two's complement notation, which makes arithmetic operations easier.
In this topic, we will consider the two's complement notation and why it is preferable to use in practice.
Getting the two's complement
In two's complement notation, a non-negative number is represented by its ordinary binary representation. The two's complement form of a negative number can be easily got from one's complement. Simply find N-bit's one's complement of a number and add 1 to it.
Two's complement is one's complement plus one.
To get the two's complement from a negative decimal number, we should perform the following simple algorithm:
- Find the binary form for the given decimal number.
- Find the one's complement by inverting all bits of the binary form (replace 0 → 1, 1 → 0);
- Add 1 to the one's complement to get the two's complement.
Suppose, we would like to get the two's complement of the decimal number -5.
- The binary form of 5 is
00000101(using the 1 byte = 8 bits); - The one's complement form is
11111010; - So, the two's complement is
11111011;
The result is a signed binary number representing the decimal value −5 in the two's-complement form. The first bit equal to 1 means that the presented value is negative.
One's complement can be converted from two's complement by subtracting 1.
Examples of some numbers
Here is a table containing examples of some first numbers in one's and two's complement forms using only 4 bits (to save space and simplify perception).
| Decimal | Unsigned binary | One's complement | Two's complement |
| -5 | 0101 | 1010 | 1011 |
| -4 | 0100 | 1011 | 1100 |
| -3 | 0011 | 1100 | 1101 |
| -2 | 0010 | 1101 | 1110 |
| -1 | 0001 | 1110 | 1111 |
| 0 | 0000 | 0000 | 0000 |
| 1 | 0001 | 0001 | 0001 |
| 2 | 0010 | 0010 | 0010 |
| 3 | 0011 | 0011 | 0011 |
| 4 | 0100 | 0100 | 0100 |
| 5 | 0101 | 0101 | 0101 |
It is not necessary to remember this table. Just make sure that you understand why this is true.
Dealing with signed zero
Why using two's complement is simpler and easier in practice?
Well, in one's complement notation, zero can be both positive (00...00) and negative (11...11) which are both inversions of each other. It brings additional problems to the computer because the computer must check for a negative zero. But since two's complement of zero is also zero, two's complement notation has no such problems!
Binary arithmetics
Another advantage of two's complement notation is that compared with one's complement notation, arithmetic operations for positive and negative numbers proceed exactly the same, so we don't need to memorize additional rules for adding and subtracting negative numbers. Try it yourself and see that it does work! Let's add 19 (00010011 in binary) and -16 (11110000 in binary).
Since we take into account only the first N bits in N-bit two's complement notation, a carry to the 9th bit, which we don't have, simply vanishes away. Let's try subtraction – subtract -3 (11111101 in binary) from 100 (1100100 in binary):
Again, a borrow from the non-existent 9th digit vanishes away. That's the difference – in one's complement binary arithmetics, the carry or borrow was wrapped around to the rightmost digit (so we were adding or subtracting 1), in two's complement it's simply ignored. Which actually makes sense: remember that two's complement of a number is its one's complement plus one? Well, here is that 1!
As you can see, binary arithmetics in two's complement notation are much easier not only for computers but also for human beings.