Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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

share|improve this answer
    
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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.