r/AskComputerScience • u/Successful_Box_1007 • Jun 21 '25
why does turning subtraction into addition using 10s complement work for 17-9 but not for 9-17 ? In the former the least significant digits match ( because we have 8 and 18) but in the latter they don’t ( we have -8 and 92)
Hi everyone, hoping someone can help me out if they have time:
why does turning subtraction into addition using 10s complement work for 17-9 but not for 9-17 ? In the former the least significant digits match ( because we have 8 and 18) but in the latter they don’t ( we have -8 and 92).
Where did I go wrong? Is 92 (from 100 - 17 = 83 then 83 + 9 = 92) not the 10s complement of 17 ?
Thanks so much!!
1
Upvotes
View all comments
Show parent comment
2
u/localghost Jun 23 '25
Yes, sorry, I made a typo with 18 and 8.
But the algorithm works just fine with subtracting a larger number from a smaller one, except you can't just manipulate the most significant digit to imitate subtracting. You actually have to do subtracting.
It works exactly because there's no trick: a number plus its 10s complement is 10 (or 100, or other number like that), by definition — and that's what the word "complement" itself means. So subtracting a number is the same as adding its complement and subtracting 10 (100, 1000, etc).
Let's look for that at another example. Say
23 - 57. The complement to 57 is 43 (they add up to 100).23 + 43 = 66. Now you have to subtract 100, but you clearly can't do it by messing with the most significant digit (in this case, it's just not there, it should be the hundreds digit). So you need66 - 100explicitly, and if that helps, you can do it by finding the complement once again, because that's what it is. Just don't forget the negative sign: -34.By the way, you can reach the same result by swapping the initial numbers, i.e. calculating
57 - 23and putting that negative sign.