# 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?

-

## 3 Answers

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

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$)

So here, I would convert to 9's complement and add one, then carry out the addition paying attention to only the last 4 digits.

-

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

Radix and Reduced Radix Complementation

Radix and Reduced Subtraction

-