4
$\begingroup$

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 3

5

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

  • 0
    whats the mod 10000 about?2011-08-28
  • 0
    @jiewmeng: It's there to indicate that we're doing [arithmetic modulo 10000](http://en.wikipedia.org/wiki/Modular_arithmetic). In particular, the $\equiv$ signs indicate the spots where we add or subtract multiples of 10000.2011-08-28
  • 0
    incomplete - where's the sign (pos/neg) of result? (OP asked about "addition/subtraction")2017-09-18
  • 0
    @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$.2017-09-18
  • 0
    thank you for responding - however that is not what I meant, see this http://gato-docs.its.txstate.edu/slac/Subject/Math/Discrete/Radix-and-Reduced-Subtraction/Radix%20and%20Reduced%20Subtraction.pdf for explanation of my previous comment. No need to respond to this comment other than maybe update your answer...2017-09-19
1

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

  • 0
    the second link ('..Subtraction') is perfect.2017-09-18
0

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.