How to subtract larger hex value from smaller hex value?

1.6k Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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}$.

0
On

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.