# How to do addition/subtraction with n complement eg. Decimal subtraction via 10s complement

How do I do in general addition/subtraction with n's complement?

With binary numbers its straightforward, convert the number you want to subtract to 1's or 2's complement then add the numbers. Handle overflow

But with n's complement, I'm quite lost. How do I do it? I believe convert the number to subtract to 10s complement then add

eg.

  1234(base10)
- 1122(base10)
--------------
1234(base10)
+ 9988(10s)
--------------
11222(base??)


I belive I did wrong? As 1234-1122=112?

To convert a negative number into ten's complement, add it to (i.e. subtract its absolute value from) $10^n$, where $n$ is the number of digits you're working with. For example, for $n = 4$,

$$-1122 \equiv 10000 - 1122 = 8878 \mod 10000.$$

An easy way to do the conversion is to first convert to nines' complement and add one:

$$-1122 \equiv (9999 - 1122) + 1 = 8877 +1 = 8878 \mod 10000.$$

You can then calculate

$$1234 - 1122 \equiv 1234 + 8878 = 10112 \equiv 112 \mod 10000.$$

• whats the mod 10000 about? – Jiew Meng Aug 28 '11 at 13:38
• @jiewmeng: It's there to indicate that we're doing arithmetic modulo 10000. In particular, the $\equiv$ signs indicate the spots where we add or subtract multiples of 10000. – Ilmari Karonen Aug 28 '11 at 14:39
• incomplete - where's the sign (pos/neg) of result? (OP asked about "addition/subtraction") – slashmais Sep 18 '17 at 9:38
• @slashmais: Are you asking how to determine whether the final result should be considered positive or negative? That's a somewhat arbitrary choice, since it just amounts to choosing a different representative of the modular congruence class. (For example, $112\equiv-9888\pmod{10000}$, since $112=-9888+10000$, so in effect $112$ and $-9888$ are the same number in 4-digit 10's complement arithmetic.) However, a common choice is to pick the representation with the smallest absolute value, so that e.g. $1122-1234\equiv1122+8766=9888\equiv-112\pmod{10000}$ is written as $-112$, not as $9888$. – Ilmari Karonen Sep 18 '17 at 15:20
• thank you for responding - however that is not what I meant, see this gato-docs.its.txstate.edu/slac/Subject/Math/Discrete/… for explanation of my previous comment. No need to respond to this comment other than maybe update your answer... – slashmais Sep 19 '17 at 14:59

I just ran across these two short papers which give a pretty good explanation of things:

For a general radix r's complement in base r, and supposing we care about m digits (this is important, as the idea is to ignore the last carry), then the r's complement ($N*$) of the number N is given by
$N* = r^m - N \mod r^m$
The r-1 complement is $N** n= r^m - 1 - N$. In fact, to go more general, if we care about m digits and n decimal digits, then
$N* = r^m - N$ and $N** = r^m - r^{-n} - N \quad\quad(\mod r^m$)