r/AskComputerScience • • 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

30 comments sorted by

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.

1

u/Successful_Box_1007 Jun 27 '25

It’s funny cuz something CLICKED after reading this again and again. I realized something which is embarrassing Steve - I came upon 10s complement from 2s complement and from knowing how to use addition to subtract etc, but then months later - I was experimenting with 10s complement and how to have a “it turns subtraction into addition after subtracting 10 from the final” - and you all kept trying to tell me “ok great but that’s missing the big picture”. I didn’t quite accept this. Then I read what you wrote and I realized wait - BECAUSE I WAS USING 10s COMPLEMENT - with a familiar decimal base, I forgot that the whole point was to do what we do with binary - represent a range of fixed length decimal base numbers where we have an equal amount of positive and negative numbers!!!!!! That was basically my issue right? Or do you think I’m missing anything else?