Two's complement and negative numbers
Introduction
In mathematics, when we need to write the sign of a number we put the sign character at the beginning of the number, more specifically we use unsigned numbers to represent the positive ones and we put the minus (-) character at the beginning to represent the negatives ones.
For example, if we want to write the number three on its positive and negative form, we should write:
However, in digital circuits there is no provision made to put a plus or even a minus sign character to a number, since digital systems operate with binary numbers that are represented in terms of “0’s” and “1’s”. When used together in microelectronics, these “1’s” and “0’s”, called a bit (being a contraction of BInary digiT), fall into several range sizes of numbers which are referred to by common names, such as a byte or a word.
Following the example, if we want to write the number three in binary on its positive form (for example in 4 bits) we should write:
But what happen when we need to write the negative form of the number in binary? As I said before, there is not the possibility to write the number on this way:
What we see above is a wrong expression, so here is when we start to talk about the two’s complement.
Two’s Complement
Two's complement is a mathematical operation on binary numbers, and is an example of a radix complement. It is used in computer science as a method of signed number representation.
This method uses the Most Significant Bit to indicate if the numbers is positive or negative, when the MSB is “1”, the number is signed as negative.
In our example the MSB is the fourth bit:
Two's complement is the most common method of representing signed integers on computers, and more generally, fixed point binary values. In this scheme, if the binary number 0011 encodes the signed integer 3, then its two's complement, 1101, encodes the inverse: −3. In other words, the sign of most integers (all but one of them) can be reversed in this scheme by taking the two's complement of its binary representation.
So, let’s see how it works.
Conversion to Two's Complement
Following the example, we were trying to represent the number three on its negative form in binary so there are a series of steps to follow in order to do that.
1st step: write the positive form of the number in binary.
Recommended by LinkedIn
2nd step: flip all the “0’s” and “1’s”.
3rd step: add one to the previous number.
So, this is how the number three on its negative form would be written in binary.
Let’s take a look to our number:
If we pay attention, the MSB is “1” and it is used to indicate the (-) sign of the number, but if we do the arithmetic with the value, we got the -3 result as we wanted (- 8 + 4 + 1 = - 3).
Inverting and adding one it's actually just a mathematical shortcut of a rather straightforward computation.
Arithmetic with Two's Complement
One of the nice properties of two's complement is that addition and subtraction is made very simple. With a system like two's complement, the circuitry for addition and subtraction can be unified, whereas otherwise they would have to be treated as separate operations.
Let’s see it with an example:
For example, let’s add 2 to 2
Now, let’s subtract 4 from 4, wich is exactly the same that adding – 4 to 4.
As we can see the arithmetic works correctly.
In summary we have talked about what is the two's complement, how it works, how we can get negatives numbers in binary using it and how addition and subtraction works perfectly.
Thank you for taking the time of reading this article, I hope you had found it interesting and helpful.