Why do some results for two’s complement subtraction requires another conversion of two’s complement again while some don’t

computer-arithmetic

Currently a student studying computer arithmetic, came across two's complement subtraction and some of the questions that I did, their answers does not another conversion of two's complement again while some have to.

This example requires a conversion for the results:

Subtract 85 from 77 in two's complement notation:

01001101 – 01010101

= 01001101 + (-(01010101))

= 01001101 + (-(10101010))

= 01001101 + (-(10101011))

= 11111000 (In decimal form this is 248, who doesn't make sense)

= 00000111 (Another two's complement conversion)

= 00001000

While this example does not have to:

Subtract 13 from 19 in 8-bit two's complement notation:

10011 – 01101

= 10011 + (-(01101))

= 10011 + (-(10010))

= 10011 + (-(10011))

= 00110

Best Answer

The best way for me to think about the two's complement is that we are calculating everything modulo $2^8$ (if you are doing it in 8 bits).

So, you start with $$77-85$$ and you first transform this into regular addition. You can do this because $$-85\equiv 171\pmod {256}$$ (note how $171_{10} = 10101011_2$, which is exactly the twos complement of $85$) which means that

$$85-77\equiv 77-(-171)=77+171\pmod {256}$$

The result is then equal to $248$, as you correctly noted.


However, there is another trick here. That is, in $8$ bit signed arithmetic, you only represent numbers up to $128$, right? Well, sure, but you also have negative numbers.

How do you recognize if a number is negative in 8 bit binary? It's when the first digit is $1$, right? Well, the first digit is $1$ if the number is greater than $10000000_2$, right? And since $10000000_2=128_{10}$, this means every number above $128$ will actually represent a negative number.

You can check for yourself that a negative number, say $-n$ where $n>0$, is (in twos complement) in fact represented in memory by the number $256-n$. This fits our modulo arithmetic, since $256-n\equiv -n\pmod{256}$. This is also how we got the $171$ to represent $-85$ in the example above, i.e., the number $-85$ in twos complement is represented by the number $10101011$, which is the number $171$.

In particular, in your case, since $$248\equiv -8\pmod 7$$ you have that the number $248$, in binary, is infact the same number as $-8$ (in twos complement).


Now, you may be thinking "I didn't calculate $171$ by calculating $256-85$! I did it by flipping all bits and adding one!"

Well, think about it this way. When you flipped all bits, you constructed a number that, when added to the original number, adds up to exactly $11111111$, right? And since $11111111_2=255_{10}$, this means that by flipping bits, you constructed the number that solves the equation $85+x=255$, or in other words, you constructed $255-85$. You then added $1$, which means the result was $$255-85+1=256-85,$$

so sure, you were flipping bits and adding $1$, but the operation "flip all bits of $x$ and add $1$ to result" is the exact same operation as the operation "calculate $256-x$. It's just that one description is easier to understand for humans, while the other is easier to do by a computer.