0
$\begingroup$

In computers we use two's-complement to subtract and it works well, but how does it work for subtraction without two's-complement when subtracting a larger number from a smaller number?

  0x0F
- 0x13
------
   -1C

Doesn't seem to work like subtracting decimal numbers would.

  01
- 11
----
 -10

It works if I flip the numbers.

  0x13
- 0x0F
------
    -4

What am I missing?

$\endgroup$
0

2 Answers 2

2
$\begingroup$

Your example for base 10 is poorly chosen. Try $3-12$. What you get is $3-2=1$ and $0-1=-1$ So the answer is $-1\cdot 10+1=-9$. You should apply the same to the hex base. $-10_{16}+B_{16}=-5_{16}$.

$\endgroup$
0
$\begingroup$

What you are missing is that the two numbers are $8$ bits each and so the subtraction results in the $8$ bit two's-complement number $0$xFC which represents $-4$. If there were more bits, then the hex number would be sign extended with FFs on the left.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.