About binary subtraction: how does this work?

76 Views Asked by At

So there's a course I'm watching online and it operates binary subtraction this way:

asking y-x, where:

y=0111 (or decimal 7)
x=0010 (or decimal 2)

And instead of using a 2's complement to change the subtrahend (x). The person in the course invert the minuend (y) and add it to the subtrahend (x). And at the end invert the result.

So that:

0111 -> 1000

1000 + 0010 = 1010

1010 -> 0101

Which is actually correct. So I was wondering how does this work ?

2

There are 2 best solutions below

0
On BEST ANSWER

2's complement is also called n-1 complement because:

-x = not(x-1) // not is 1's complement (bit-wise). 

The mentioned computation can thus be restated as:

not (not(y) + x) 
        = not (-(y+1) + x) 
        = -(-(y+1) + x + 1) 
        = -(-y + x) = y - x
0
On

The "inversion" is ones complement. The ones complement of an $n$-digit binary number $x$ is $$ 2^n - 1 - x $$ whereas the twos complement is $2^n - x.$

So the method shown in the course is to take the ones complement of $x$, add it to $y$, and take the ones complement of the result.

Ones complement of $x$ added to $y$:

$$ (2^n - 1 - x) + y = y - x + 2^n - 1. $$

Ones complement of the result:

$$ 2^n - 1 - (y - x + 2^n - 1) = 2^n - 1 - y + x - (2^n - 1) = x - y. $$