Prove decimal number pattern for its n-bit 2's compliment

867 Views Asked by At

If we let $B = b_{n−1}b_{n−2} · · · b_1b_0$ be an $n$-bit $2$’s complement integer. How can we show that the decimal value of $B$ is $−b_{n−1}\cdot2^{n−1} + b_{n−2} \cdot 2^{n−2} + b_{n−3}\cdot 2^{n−3} + \cdots + b_1\cdot 2 + b_0$.

1

There are 1 best solutions below

11
On BEST ANSWER

Hint: let $\;A=-B\;$ then, by the definition of $2$'s complement, $A$ is obtained from $B$ by flipping all bits and adding $1\,$:

$$ A = (1-b_{n-1})\cdot 2^{n-1} + (1-b_{n-2})\cdot 2^{n-2} + \cdots + (1-b_1) \cdot 2 + (1-b_0) + 1 $$

Let $B\,'$ be the given expression $B\,' = −b_{n−1}\cdot2^{n−1} + b_{n−2} \cdot 2^{n−2} + \cdots + b_1\cdot 2 + b_0\,$. Then:

$$ \require{cancel} \begin{align} A + B\,' & = - 2 b_{n-1}\cdot 2^{n-1} + (2^{n-1}+2^{n-2}+\cdots+2+1) + 1 = \\ & = -b_{n-1} \cdot 2^{n} + (2^n - \cancel{1}) + \cancel{1} \\ & \equiv \;0 \pmod{2^n} \end{align} $$

Since $2$'s complement arithmetic is $\bmod 2^n\;$ it follows that $A+B\,'=0$ $\iff$ $B\,'=-A=B$.