Why does the $2$'s and $1$'s complement subtraction works?
To give you the intuition I will focus on non-negative numbers $x,y\ge 0$ and that the numbers are $k$-bit integers.
1-complenet
To get the 1-complement from a number $x$ you flip every bit in $x$s binary representation $\text{bin}(x)$, 1
$\Leftrightarrow$ 0
. You can do the same, by subtracting $x$ from the binary number that contains only 1
s and is as long as $\text{bin}(x)$. For example,
111111
-010111
-------
101000
This works for every single bit, and there are no carries, so it works for the whole binary representation.
Thus we can describe the 1-complement of $x$ by $c_1(x):=(2^{k+1}-1)-x$, where $k$ is the number of bits we have per number. By definition of the 1-complement we have $c_1(c_1(x))=x$.
Assume that $x>y$ (rule 4), hence when subtracting, we will have an overflow. Thus we will add $+1$ to the result, which yields
$$ x+c_1(y)+1=x+(2^{k+1}-1)-y+1=(2^{k+1})+(x-y),$$
since we consider only the first $k$ bits, the 1
representing $2^{k+1}$ will vanish, and we get $x-y$.
Assume now that $x\ge y$ (rule 3). Then there is no overflow and $$ c_1(x+c_1(y))=c_1 (x+(2^{k+1}-1)-y)=c_1(c_1(y-x))=y-x=|x-y|.$$
2-complement
To get the 2-complement of an number $x$ we flip the bits, starting after the right most 1
. This can be interpreted as subtracting $x$ from $2^{k+1}$. Again we look at an example
1000000
-0010100
--------
101100
Until the first 1
we don't have carries, so the bits are just taken from $x$, after the first 1
, we have always a carry, and hence the bits get flipped.
Similarly to the 1-complement let $c_2(x):=2^{k+1}-x$, and again $c_2(c_2(x))=x$.
Assume that $x>y$ (rule 4), this gives $$ x+c_2(y)=2^{k+1}-y+x=2^{k+1}+(x-y).$$ Again, the $2^{k+1}$ vanishes since we consider only the first $k$ bits.
Assume that $x<y$ (rule 3), then $$ c_2(x+c_2(y))=c_2 (x+2^{k+1}-y)=c_2(c_2(y-x))=y-x=|x-y|.$$
Both concepts can be extended (on a very natural way) to work with negative numbers.