How does the $2$'s complement describe a negative number in binary?

1.2k Views Asked by At

According to my knowledge, the 2's complement is used to describe a negative number in binary representation. But I have this confusion.

Example: Suppose that we are using 5 bits registers. The representation of -5 and +5 is as follows:

+5 is represented as it is represented in sign magnitude method. -5 is represented using the following steps:

(i) +5 = 0 0101

(ii) Take the 2’s complement of 0 0101, which is 1 1011. The MSB is 1, which indicates that number is negative. (The MSB is always 1 for negative numbers.)

(The example is described clearly below.)

The question is described clearly here

My question: In this example, 0101 is 5. But when we find its negative binary form, it is 1011.

But 1011 is equivalent to 11 in decimal. How can two decimal numbers be represented by the same binary representation, which is 1011?

2

There are 2 best solutions below

1
On

The number $11011_2$ is not meant to be read as a binary number "in the usual sense", as you seem to assume. If you really wanted a number like that, with a sign bit and then five in binary, that'd be $10101_2$, which is the what -5 is with one's complement.

In the two's complement system, we take another number as negative, which simplifies the addition/subtraction process. In a stricter mathematical sense we take the number mod $2^N$ where $N=5$ in this case. ($-5 = 27 \mod 32$)

2
On

Unsigned five bit binary can count all integers from 0 to 31 (inclusive). If we want signed five bit binary then we have to choose how we want to represent the negative numbers.

One way is to emulate how we usually do it in decimal: We have the sign and then we have the absolute value. This would make $5$ into $0\,0101_2$ and $-5$ into $1\,0101$. This method is pretty straight-forward and probably most-easily human readable. But arithmetic is more complicated than it has to be. Which is important if you want cheap, fast, small and easy to produce processors.

The approach that has turned out to be the most efficient in that respect is two's complement. Here are two ways to look at it:

  • Negating a number is done by flipping all the bits, then adding 1 the way you normally would. This is very practical, but doesn't foster much understanding.
  • Think about unsigned binary modulo some power of two, then relabel half of them accordingly to make them negative. This is a little more abstract, but this way it's much easier to see why it works, once you're comfortable with modular arithmetic.

Let's work out the representation of $-5$ in your five-bit example. We start with the representation of $5$, which is $00101_2$. Then we flip all the bits to get $11010_2$. Then we add $1$ to get $11011_2$. That's the five-bit two's complement representation of $-5$.

As for the second interpretation, this is what I mean (again with your 5-bit example): We work modulo $2^5$, because that's how many bits we have. We list all the unsigned numbers together with their binary representation, then we reinterpret the binary representations in the larger half to instead mean numbers that are $2^5$ smaller: $$ \begin{array}{|c|c|c|}\hline \text{Unsigned}&\text{Binary}&\text{2's complement}\\\hline 0&00000&0\\ 1&00001&1\\ 2&00010&2\\ \vdots&\vdots&\vdots\\ 14&01110&14\\ 15&01111&15\\ 16&10000&-16\\ 17&10001&-15\\ 18&10010&-14\\ \vdots&\vdots&\vdots\\ 30&11110&-2\\ 31&11111&-1\\\hline \end{array} $$ The great thing about this is that negation is pretty simple from a chip standpoint, and subtraction can actually be done by negating the second number and just add the two numbers regularly (compare to the "sign and absolute value" approach, where subtracting $5$ is not done the same way as adding $-5$). Which means you don't really need dedicated subtraction circuits. Which is awesome. It is also very easy to detect addition overflow: it happens exactly when the carry into the sign bit is different from the carry out of the sign bit.

(And just for completion, you also have the one's complement, where the negation process skips the "add 1" step at the end. This makes it so that the maximal representable number is exactly the negative of the minimal representable number, which might be nice. And also, negation does become simpler. But on the other hand, $+0$ and $-0$ are distinct (one represented as $00000_2$ and the other as $11111_2$), and you have to do some additional correction every time you add with a negative number. So it is not as popular.)