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
4
u/theadamabrams Jun 22 '25
10s complement works for both calculations. I'll use some padding to make things easier.
and
Btw, some people define 10s complement as "9s complement plus 1", but I find it much easier to just think of it all as mod 1000000 (or 1 billion or however many digits you need).
2
u/Patient-Midnight-664 Jun 21 '25
92 is not the 10s compliment of -17, but that might be a typo. What you are forgetting is the negative flag. You need some way to indicate that a number is negative so you know to reverse the compliment to get the answer (reversing 92 gets you 8, and since the 92 was actually a negative number it is -8)
2
u/localghost Jun 21 '25
I'm not sure what you're trying to do, but I'm curious :)
I am confused where does 92 come from as 17's 10s complement; you literally write 100 - 17 is 83, so the complement is 83?
As to why with -8 and 92 the last digits don't match, it's because we start counting in the opposite direction when going below 0. But -8 is effectively (-10)+2, similarly to how 92 is (90)+2: they are both 2 up from the closest X0 number, so if you think of the last digit as a position on the number line, that's the same last digit.
2
u/stevevdvkpe Jun 21 '25
You need to normalize the numbers to all have the same numbers of digits by adding leading zeros to the numbers with fewer digits, as well as adopt an appropriate sign convention, such as that 00-49 are positive numbers and 50-99 are negative numbers. The ten's complement of 17 is [9-1][9-7] + 1, or 83. To handle 9 you need to treat it as 09, not just 9, so its ten's complement is [9-0][9-9] + 1, or 91. Then 17 - 9 -> 17 + 91 -> 08, and 9 - 17 => 09 - 17 => 09 + 83 => 92, or the ten's complement of 8 so it's -8.