[Math] addition of two negative numbers using signed 10’s complement

arithmeticbinary

the question given was:-
(-9742)+(-641)
since we take the complement of the negative numbers, the 10's complement of first number was 258 and for the second it was 9359, but after adding both of these two complements the result is 9617 to which i further subtract by the 10000 to get -383. i wanted to know where i am going wrong because the correct answer will be -10383? is my approach wrong? if yes, then what's the correct method?

Best Answer

Your problem is that the computation overflows when you're using only 4 digits. Even your input is out of range: Representing $-9742$ as 0258 leaves no clue that the original input was actually $-9742$ rather than $258$.

The way out of this is to use enough digits that both the inputs and the result has a sign digit at the front. With 5 digits $$ (-9742)+(-641) = ? $$ gets represented as $$ \mathtt{90258} + \mathtt{99359} = {}_1\mathtt{89617}$$ where the small ${}_1$ denotes a carry out that you ignore because you're working with a word lenght of 5 digits only.

Assuming there was no overflow (which there isn't in this case, though it might have been prudent to use yet another digit), the leading 8 tells us that the result is negative (if we're assuming a symmetric overflow interval this is the case when the leading digit rounds up).

We can then translate 89617 back to what it represents: $89617-100000=-10383$ as it should be.


This assumes that you're using 10s complement as a didactic tool to understand computer arithmetic, which uses 2s complement with fixed word length. If we're approaching this from a more mathmematical angle, the principled thing would be to have infinitely many digits, with numbers stretching to the left as long as the digits are eventually all 9 or eventually all 0.

(In fancier words, this corresponds to calculating with the homomorphic image of $\mathbb Z$ in the 10-adic integers).

We would then represent $$ (-9742) + (-641) = {?} $$ as $$ {.}{.}.9990258 + {.}{.}.999359 = {.}{.}.99989617 $$ and then we don't need to hedge stuff about overflow, because this result unambiguously represents $-10383$.

Related Question