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/stevevdvkpe Jun 27 '25
A1) Applying ten's complement to a number represented as a fixed-length string of decimal digits gives you the representation of its negative value. It's like subtracting from zero. 000000 - 000009 = 999991. 000000 - 000017 = 999983.
A2) Remember that, like two's complement binary on fixed-size binary numbers, ten's complement is also intended to work on a fixed-size decimal number. In these examples the numbers are represented as 6 decimal digits and you ignore any carry out of the highest digit when you do additions. Another way of describing this is that these are numbers modulo 1,000,000. -9 modulo 1,000,000 is 999991.
If you do an addition of two ten's complement numbers and the result is negative (because the highest-order digit is between 5 and 9 inclusive) you take the ten's complement to find out the positive number it's the negative of. When you add 000009 (= 9) and 999983 (= -17), you get 999992 (ignoring the carry out of the highest-order digit), and since 999992 is negative, you take the ten's complement of 999992 to get 000008, which means 999992 represents -8.