What's the proper way to subtract a negative number with two's complement?

321 Views Asked by At

Title says all. Is it just ordinary addition? Because that seems like it's too simple.

1

There are 1 best solutions below

0
On BEST ANSWER

Same as any other subtraction with two's complement: $a-b$ is computed by adding $a$ to $-b$. If $b$ is negative, then its negation $-b$ is positive, but that doesn't really change anything. The steps are the same: first negate $b$, then add the result of that to $a$.

The only arguable corner case is if $b$ is the most negative representable number. Then the true value of $-b$ is not representable, but it happens to work out correctly even then just by doing ordinary 2's complement negation: If the true value of $a-b$ is representable (which it will be if $a$ was negative too), then its representation is what the binary addition will produce; otherwise the result will be the representable number that is congruent to the true result modulo $2^w$.