How would you solve: 6 - 10?
Since 10 > 6, you know the answer has to be negative and you would compute the result by changing it to:
10 - 6 = 4 and then change the result to -4.
That is basically what you did to solve it. One thing to keep in mind when you're dealing with binary numbers is signed vs. unsigned (2's complement).
For your example:
8 - 13 = -5
0x8 - 0xd = 0xb
1000 - 1101 = 1011
Signed Interpretation:
1000 - 1101 = 1011
-8 - (-3) = -8 + 3 = -5
Unsigned Interpretation:
1000 - 1101 = 1011
8 - 13 = 11 (Incorrect because you can't represent negative numbers)
So I've run into an exercise where I am required to solve the following:
0x80000000 - 0xD0000000
I'm really not sure what to do in this scenario. I know we could just borrow 16 from somewhere if we were subtracting a smaller number from a larger one, but I don't see where I'm supposed to borrow numbers. Obviously the answer will be negative. It wouldn't be too hard to convert to binary and solve it that way, but I would much rather figure out how to do this without having to convert into another base.
The correct answer should be 0xB0000000. I was able to obtain this answer with the following logic:
Find the difference between 8 and D:
D - 8 = 5
Since we are going negative:
16 - 5 = 11 = B
So the answer is:
0xB0000000
This method worked in this case, but I'm afraid that it may not generalize to other problems I may encounter. Can someone provide me with some advice on how I can solve problems like this with a more mathematical approach?