Some doubts in 2's complement

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$

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$)


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).