Some doubts in 2's complement

3.5k Views Asked by At

I am doing one question in which we have to find 2's complement of 43. I know to find 2's complement we have to find 1's complement and then add 1 into it to get 2's complement.

So here is how I am doing it -

Binary value of 43 is 00101011. (We have to use 8-bits to represent)

1's complement is 11010100.

2's complement is 11010101.

But the original answer is 00101011.

I used some online converter and they are giving same answer as the original one. But i don't find any mistake in my method. So please help me to understand my mistake and how to get the original answer.

Edit -

This is the link to online converter : Click Here.

Update -

As said by one user that my question is wrong or it is wrong in textbook. It is not. Same wording of question above in my question. Here's the link from which I am solving this question before.

Click here to see.

And I wrote answer later from this link. As you can see in my answer below.

8

There are 8 best solutions below

3
On BEST ANSWER

Your answer is correct. In two's-complement representation, positive numbers are simply represented as themselves, and negative numbers are represented by the two's complement of their absolute value.

Here's Wikipedia explanation you can see.

0
On

The $8$ bit two's complement of $$43_d=00101011_b$$ is undisputably $$256_d-43_d=213_d=11010101_b.$$

Your inapprehension comes form the fact that this "Two’s Complement Calculator" does not compute the two's complement of a number, but shows you the two's complement representation of that number. Namely, $0\to00000000,1\to00000001,-1\to11111111\cdots$

As the change of the sign of a number corresponds to its two's complement, you correctly get $-43_d\to11010101_b$.

0
On

The problem with the term 2s complement is that there's some ambiguity. On wikipedia it says:

Two's complement is a mathematical operation on binary numbers, as well as a binary signed number representation based on this operation.

The problem is that the mathematical operation is as you mention the operation of inverting and adding one. The other on the other hand is taking the binary representation of the number using the 2s complement operation to represent negative numbers.

This gives two different results as you've seen. The former gives your answer while the second meaning gives the other answer.

0
On

From one post I got this -

In 2's complement representation, positive numbers are represented as their representation and negative numbers are represented by first doing 1's complement, then adding 1 to the result. So 43 is represented as 00101011.

Hope it's correct. If still I am missing something please let me know.

0
On

Mwahaha! You got confused between $43$ and $-43$. I take delight in such confusions.

To add to your confusion, the one's complement of a a positive integer is the same as a bitwise XOR of that integer with $2^\mathcal{L} - 1$, where $\mathcal{L}$ is the number of bits, $8$ in this case.

So, bitwise XOR($+43, +255$) gives us $212$, or $11010100$ in binary. So far you're correct. Then we add $1$ to get $11010101$, which is $213$, or $11010101$ in an unsigned byte. But if that's a signed byte, then $11010101$ means $-43$.

Go back to your online converter, and try it first with $-43$ and then $+43$. If it's been properly programmed and you input the numbers correctly, it should give you $11010101$ for the former and $00101011$ for the latter.

If you're on Windows, try the Windows Calculator in Programmer Mode (View > Programmer). It should give you $101011$ for $43$ and $1111111111111111111111111111111111111111111111111111111111010101$ for $-43$ (you can choose between bytes, words, double words and quadruple words).

0
On

I think your textbook did not word the question clearly enough.

What is the 8-bit representation of 43?

The answer is then 00101011.

What is the 8-bit representation of $-43$ using two's complement?

Then the answer is 11010101, just as you figured out. For any other phrasing of the question, you can probably convince the teacher to give you the point.

Notice also that you can get the original number back by applying the same process: one's complement of 11010101 is 00101010, then you add 1 and get 00101011.

One more way to check is by taking 256 (or 65536 or 4294967296 or 18446744073709551616 as applicable) and subtracting the positive integer. The unsigned representation of that number should be the same as the signed representation of the negative integer.

Is one of your doubts why we bother with this complicated system at all? Wouldn't it be easier to just set the sign bit as needed? e.g., $-43$ would be 10101011.

But then we'd have two zeroes: 00000000 and 10000000. One of the most common things computers do all the time is check whether a given number is 0. But if 0 can have two different representations, things get more complicated.

The asymmetry created by two's complement (the byte runs from $-128$ to 127 rather than 128) is a very small price to pay for the convenience of being able to check that a given number is 0 by simply checking all the bits are off.

0
On

You are right. Here the details.

Let's go from the other side, that is, let's start from the definition of two's complement notation, so we can determine what number is represented by a given two's complement notation, and then find the inverse relation.

Definition of two's complement notation.

$n$-bit two's complement notation of an integer $a$ is a representation of such an integer through an ordered sequence of $n$ bits $\{t_i\}_{i=0,\ldots,n-1}$ such that: $$a=-t_{n-1}2^{n-1}+\sum\limits_{i=0}^{n-2}t_i2^i\tag{1}$$

So this relation gives a map: $\phi_{2\text{'s}}:\{0,1\}^n\rightarrow[-2^{n-1},2^{n-1}[\cap\mathbb{Z}$

This map is invertible, that is, given an integer in the range $[-2^{n-1},2^{n-1}[$ there is one and only one $t\in\{0,1\}^n$ such that $(1)$ is verified.

Let's find its inverse.

Notice that $(1)$ can be rewritten as: $$a=-t_{n-1}2^n+\sum\limits_{i=0}^{n-1}t_i2^i\tag{2}$$

It is known that by unsigned integer binary notation of an integer it is meant a representation of such an integer through an ordered sequence of $n$ bits $\{u_i\}_{i=0,\ldots,n-1}$ given by the inverse of such a map:

$$\phi_b:\{0,1\}^n\rightarrow[0,2^n[\cap\mathbb{Z},~u\mapsto \phi_b(u)=\sum\limits_{i=0}^{n-1}u_i2^i$$

So in $(2)$ the second term at the right-hand side is an integer $b$, $0\le b<2^n$ whose unsigned binary notation happens to coincide with the two's complement notation of $a$: $\phi_{2\text{'s}}^{-1}(a)=\phi_b^{-1}(b)$. Then from here and $(2)$ again, it follows that the inverted map is given by:

$$\phi_{2\text{'s}}^{-1}(a)=\phi_b^{-1}(a+t_{n-1}2^n)\tag{3}$$ but there is still the unknown $t_{n-1}$.

So from $(2)$, $t_{n-1}=0$ is equivalent to saying that $a\ge0$ because $b\ge0$ and $t_{n-1}=1$ is equivalent to saying that $a<0$ because $b<2^n$

In the end $(3)$ is fully determined by:

$$\phi_{2\text{'s}}^{-1}(a)=\begin{cases}\phi_b^{-1}(a)&a\ge0\\\phi_b^{-1}(a+2^n)&a<0\end{cases}\tag{4}$$

Conclusion.

This explains why you don't have to "modify" positive numbers, just extract the binary notation; and why you instead need to add $2^n$ to negative numbers and extract the binary notation (or in two steps, one's-complement each bit of its binary notation and then add $1$)

1
On

You were correct and the textbook is still wrong. Not surprising given what a slapdash job they did of putting it together. I'm quoting from the second page you linked:

Explanation: In$2$’s complement representation, positive numbers are represented as their representation and negative numbers are represented by first doing $1$’s complement, then adding $1$ to the result.

Notice the lack of a space between "in" and "$2$’s"? Just that little detail gives me serious doubts as to whether they even know what the hell they're talking about.

Well, to be fair, they do know what they're talking about, they just don't know how to explain it simply and correctly. "Represented as their representation"? What does that even mean? "And negative numbers are represented by first doing $1$’s complement" on what? On their absolute value or on some random number?

So $43$ is represented as $00101011$.

Note that option represents $-43$.

Are they saying that $00101011$ represents both $43$ and $-43$? Maybe they meant to write something else between those two sentences but just didn't get around to it.

The answer is indeed $11010101$ like you got, which means $-43$ in a signed $8$-bit representation using two's complement, or $213$ in an unsigned $8$-bit representation. If they wanted $00101011$, they should have asked for the the $8$-bit representation of $43$.

And lastly, what sort of material are you supposed to read and understand before taking the quiz? Here's an explanation of two's complement from Cornell University: https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html

Although the author of those Cornell notes says the topic doesn't need a lengthy explanation, I think you will find his explanation far more detailed, correct and easy to undestand than anything on GeeksForGeeks.org.

And if after reading that you still have "doubts," try the following exercises with pencil on paper:

  • $00000101 + 00000011$
  • $11010101 + 00000011$
  • $11011000 - 00000011$
  • $11010101 \times 11111111$