how to solve two's complement question?

561 Views Asked by At

i tried 9 D + (-10 D)

9= 0000 1001

10= 0000 1010

Reverse 10 = 1111 0101 and add 1 become 1111 0110

after that add up 9 D + (-10 D) == 0000 1001 + 1111 0110 but the answer is equal to 1111 1111 whch is 255 in decimal but the answer should be -1 right? anything goes wrong?

Thank you very much.

2

There are 2 best solutions below

0
On BEST ANSWER

In two's complement representation for binary numbers, the number 1111 1111 represents -1. You missinterpreted the result as a "normal" binary number.

In two's complement, binary numbers of $2^n$ bits represent values ranging from $-2^{n-1}$ to $2^{n-1}-1$.

3
On

In two's complement, we deal with signed numbers by checking the leftmost bit. If this leftmost bit is a $0$, then it's positive, so we proceed like we do with unsigned numbers. Otherwise, if the leftmost bit is a $1$, then it's negative, so we have to do the "reverse the bits then add one" trick that you did when you converted $-10_{10}$ to binary. In general: $$ -x = \overline x + 1 \iff x = -(\overline x + 1) $$ Hence, since $1111~1111$ starts with a $1$ and we are dealing with two's complement, we have: \begin{align*} (1111~1111)_2 &= -((\overline{1111~1111})_2 + 1) \\ &= -((0000~0000)_2 + 1) \\ &= -((0000~0001)_2) \\ &= -1_{10} \end{align*}