 # Arithmetic operations

A number expressed in base or radix N consists in digits ranging from 0 to N-1. A valid octal (base-8) number can be for example, 6174. Number expressed in any base can be added or subtracted by following the same (usually unknown rules) as done for decimals operations.

## Sum of Base-N numbers

The sum is performed digit by digit starting from right to left until the last digit is added. The rules are:

• If the sum of all digits in the same position is greater or equal than the base, then the value of the base must be subtracted and a carry is generated.
• Repeat until the sum is less than the base. The carry is increased by one each time the base is subtracted.
• If a final carry is left after the last digit to the left, then it must be placed before the first number of the result.

Example: Add the hexadecimals 4FC + EB5. Recall the values of the letters: A=10, B=11, C=12, D=13, E=14, F=15.

• Start by adding the rightmost digits: C+5. Using the decimal equivalent: C+5=12+5=17
• Subtract 16. 17-16=1. A carry digit of 1 is generated.
• Add the following digits including the carry: F+B+1=15+11+1=27
• Subtract 16: 27-16=11 --> B, A carry of 1 is generated
• Add the leftmost digits including the carry 4 + E + 1 = 4 + 14 + 1 = 19
• Subtract 16: 19-16=3. The last carry digit is placed first. Result: 13B1

## Subtraction of Base-N numbers

For the sum, the subtraction follows the same rules as for decimals.
Source: The Math Forum

```            Suppose we want to subtract A8D2 - 3EAC (hexadecimal). We'll align our numbers:

A  8  D  2  h
- 3  E  A  C  h
-------------

Now in the ones place, we can't subtract C (12) from 2 so we borrow 1
from the sixteens place. Since that has a place value (a "weight") of
sixteen, we can exchange it for 16 ones. (This is kind of like
changing a \$16 bill into sixteen \$1 bills.) This leaves us with 12
sixteens (D = 13 minus the 1 we borrowed) and gives us 18 ones (2 plus
the 16 we got from the borrow). We now subtract 18-12 = 6:

(Note that I use decimal here. Some people
12 18       write these as Ch and 12h.)
A  8  \  \  h   (There was a D and a 2 under the '\'s)
- 3  E  A  C  h
-------------
6  h

In the sixteens place, we don't need to borrow because we can subtract
10 (A) from 12:

12 18
A  8  \  \  h
- 3  E  A  C  h
-------------
2  6  h

In the 256's place, we again need to borrow. We'll borrow 1 from the
4096's place and exchange it for sixteen 256's (one 4096 equals
sixteen 256's). This leaves us 9 in the 4096's place (A = 10 minus the
1 that we borrowed), and gives us 24 in the 256's place (8 plus the 16
from the borrow). We then can subtract 24-14 = 10 = A. So we have:

9 24 12 18
\  \  \  \  h
- 3  E  A  C  h
-------------
A  2  6  h

Finally, we subtract 9-3 = 6 in the 4096's place:

9 24 12 18
\  \  \  \  h
- 3  E  A  C  h
-------------
6  A  2  6  h

Why did we get 16 from each borrow? Because each time, the next place
value was 16 times as large. This is because hexadecimal is base-16.

One final note: If the subtrahend (the bottom number) is larger than
the minuend (the top number), flip the numbers around and make the
final answer negative, just as you would in decimal.
```

The subtraction for other bases follow the same rules, taking into consideration that the borrow adds up 8 for octals and 2 for binaries.