How to subtract larger hex value from smaller hex value?

$\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

$\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$ $\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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like