Prerequisite
Knowledge of the concepts from the tutorial
Base n Numbers
are
a prerequisite for this tutorial
Section 1 Addition of Base-n Numbers
The trick to doing base-n addition is to "fake" it by doing decimal arithmetic
in your head or on a piece of scratch paper -but writing down answers
in base-n.
Review of Decimal Addition
Procedure to Add Two Base-n Numbers
Example Adding Base-n Numbers
Review of Decimal Addition (1.1)
Here are the principles of adding two decimal numbers:
- Line up the numbers from right to left. If one number is shorter,
extend it by adding
leading zeros to the front of the shorter number.
- Work from right to left - doing the same addition and carry operation
in each column.
- Add the two digits in the same column and also add the carry at the
top of the column (if any).
- A one digit answer is simply recorded at the bottom of the same column.
- A two digit answer requires you to subtract 10 from the sum and record
the remainder at the
bottom of the same column.
- A two digit answer also requires you to record a carry of 1 in the
next column over to the left.
- Any extra carry is recorded in a new column on the left.
Of course no one actually does every step for subtracting 10. Instead they
split the answer into two digits, record the right hand digit in the same column
and carry the left hand digit into the column over one place left.
HOWEVER, you need to get used to thinking of the step as "subtracting ten".
So for example:
- Suppose the sum in a column was 16
- An answer of 16 is two digits.
- Subtract 16 - 10 to get 6.
- Record the 6 in the same column.
- Record the 1 as a carry in the next column to the left.
Procedure to Add Two Base-n Numbers (1.2)
Here is how to add together two (unsigned) base-n numbers:
- Line up the numbers from right to left. If one number is shorter
extend it by adding
leading zeros to the front of the number.
- Work from right to left - doing the same addition and carry operation
in each column.
- Add the two digits in the same column and also add the carry at the
top of the column (if any).
- The digits may be A,B,C etc and if so, mentally
convert them
to decimal before adding.
- Remember to record a carry digit as explained below - even if it
is zero.
- Keep the decimal sum in your head - or write it down on some scratch
paper.
- Compare the sum (in your head) with the radix n.
- A sum less than n is just recorded at the bottom of the same column.
- Be careful to write the sum as a
single base-n digit.
- Write a 0 as the carry in the next column left.
- A sum greater or equal to n requires several steps:
- Subtract (in decimal in your head) the radix n from the sum of the digits
you already computed.
- Record the difference as a base-n
digit at the bottom of the same column.
- Write a 1 as the carry in the next column left.
- Any extra carry past the leftmost column is not recorded
in the answer. It is called the carry out and is usually stored
somewhere else on the computer.
The answer must have the same number of digits as the two operands.
Example Adding Base-n Numbers (1.3)
Here is an example of adding B2C59 and 346A2 in base 13 :
carry row: 1 0 1 1 0
first number: B 2 C 5 9
second number: 3 4 6 A 2
-------------- ---------
sum: 1 7 6 2 B
With a detailed explanation:
- Start at the right and add 9 and 2 to get 11. However
- write the 11 as a B since that is a base-13 digit. There is a zero
carry in the next column over because the 11 is less than the base
13.
- Second from the right Add the carry (0) and the 5 and the
A, but remember that the A is really 10. You get 0+5+10 = 15. This
is too large (greater than or equal to 13). So subtract the base: 15-13 =
2 and record the 2 into the answer at the bottom. Also record a carry
of 1 in the next column left.
- Third from the right Add the carry (1) and the C and the
6, but remember that the C is really 12. You get 1+12+6 = 19. This
is too large (greater than or equal to 13). So subtract the base: 19-13 =
6 and record the 6 into the answer at the bottom. Also record a carry
of 1 in the next column left.
- Fourth from the right Add the carry (1) and the 2 and the
4. You get 1+2+4 = 7. This is less than 13 and so is recorded into
the answer at the bottom. Also record a carry of 0 in the next column left.
- Fifth from the right Add the carry (0) and the B and the
3, but remember that the B is really 11. You get 0+11+3 = 14. This
is too large (greater than or equal to 13). So subtract the base: 14-13 =
1 and record the 1 into the answer at the bottom. Also record a carry
of 1 in the next column left.
- The last carry bit (1) is not recorded
in the answer. It is called the carry out and is recorded elsewhere
in the computer.
Section 2 Subtraction of Base-n Numbers
The trick to doing base-n subtraction is to "fake" it by doing decimal arithmetic
in your head or on a piece of scratch paper -but writing down answers
in base-n.
Revision of Decimal Subtraction
Procedure to Subtract Two Base-n Numbers
Example Subtracting Base-n Numbers
Revision of Decimal Subtraction (2.1)
Here is a new way to subtract decimal integers:
- Line up the numbers from right to left. If one number is shorter
extend it by adding
leading zeros to the front of the number.
- Work from right to left - doing the same subtraction and borrow operation
in each column.
- Subtract the two digits in the same column and also subtract the
borrow at the top of the column (if any). You may get a negative answer.
This is OK, do not borrow a ten to avoid the negative answer - just
do the signed arithmetic.
- If the difference is positive or zero - then just record it at the
bottom of the column and record a borrow 0 in the next column left.
- If the difference is negative - then add 10 to it and record the
result (which will no longer be negative) at the bottom of the column. Also
record a borrow 1 in the next column left.
Try this out for a problem like 3100-2957 and then also do it the way you learned
in grade school. You should get the same answer if you are doing it right.
Procedure to Subtract Two Base-n Numbers (2.2)
Here is how to subtract two (unsigned) base-n numbers:
- Line up the numbers from right to left. If one number is shorter
extend it by adding leading zeros
to the front of the number.
- Work from right to left - doing the same subtraction and borrow operation
in each column.
- Subtract the two digits in the same column and also subtract the
borrow at the top of the column (if any).
- The digits may be A,B,C etc and if so, you mentally
convert them
to decimal before subtracting.
- Remember to record a borrow digit as explained below - even if it is
zero.
- The difference may be negative.
- Keep the decimal difference in your head - or write it down on some
scratch paper.
- A zero or positive difference is just recorded at the bottom of the
same column.
- Be careful to write the sum as a single
base-n digit.
- Write a 0 as the borrow in the next column left.
- A negative difference requires several steps:
- Add (in decimal in your head) the radix n to the difference you already
computed. The result should no longer be negative.
- Record the result as a base-n
digit at the bottom of the same column.
- Write a 1 as the borrow in the next column left.
- Any extra borrow past the leftmost column is not
recorded in the answer. It is called the borrow out and is
usually stored somewhere else on the computer. The answer must have
the same number of digits as the two operands.
Example Subtracting Base-n Numbers (2.3)
Here is an example of computing B2C59 less 346A2 in base 13 :
borrow row: 0 1 0 1 0
first number: B 2 C 5 9
second number: 3 4 6 A 2
-------------- ---------
difference: 7 B 5 8 7
With a detailed explanation:
- Start at the right and
compute 9 minus 2 to get 7.
This is positive/zero, so record it at the bottom
and put a borrow of zero one column over.
- Second from the right
Now compute 5 - 10 - 0 since the digit A
is really 10 and the borrow is 0.
Your answer is -5 which is negative.
Thus add the base 13 to -5 to get 8 and record this
at the bottom and put a borrow of one in the next column left.
- Third from the right
Now compute 12 - 6 - 1 since the digit C
is really 12 and the borrow is 1.
Your answer is 5 which is positive/zero, so record it
at the bottom and put a borrow of zero one columm left.
- Fourth from the right Now compute 2 - 4 - 0 since the borrow
was 0. This gives -2 which is negative. Thus add the base 13 to -2 to get
11 and record that at the bottom as the single digit B. Also place
a borrow of one into the next column left.
- Fifth from the right
Compute 11 - 3 - 1 since the digit B is
really 11 and the borrow is 1.
The result is 7 which is positive/zero and so record
it at the bottom and put a borrow of 0 over one column.
- The last borrow bit (0) is not recorded
in the answer. It is called the borrow out
and is recorded elsewhere in the computer.
Section 3 Digit Min and Max Operations
Except for binary numbers, these operations are fairly useless. However, they
are very simple to do:
Digit Minimum Operation
Digit Maximum Operation
Digit Minimum Operation
This operation takes the smallest of corresponding pairs of digits.
- Line up the numbers from right to left. Both numbers MUST
have the same number of digits. The operation is not defined otherwise.
- Work in any direction you want - doing the same operation in each
column.
- Find the smallest of the two digits in the same column and place
that in the answer at the bottom of the same column.
- There is no carry or borrow or any kind of connection between different
columns. This is called a digit-wise operation.
Here is an example of the digit min of B0C59 and 34A52 in base 13 :
first number: B 0 C 5 9
second number: 3 4 A 5 2
-------------- ---------
digit min: 3 0 A 5 2
This is simple enough not to need a detailed explanation.
Digit Maximum Operation
This operation takes the largest of corresponding pairs of digits:
- Line up the numbers from right to left. Both numbers MUST
have the same number of digits. The operation is not defined otherwise.
- Work in any direction you want - doing the same operation in each
column.
- Find the largest of the two digits in the same column and place that
in the answer at the bottom of the same column.
- There is no carry or borrow or any kind of connection between different
columns. This is called a digit-wise operation.
Here is an example of the digit min of B0C59 and 34A52 in base 13 :
first number: B 0 C 5 9
second number: 3 4 A 5 2
-------------- ---------
digit max: B 4 C 5 9
This is simple enough not to need a detailed explanation.
Section 4 Binary Logic
When performed with binary numbers, the digit
min and digit max operations
have special names. The three common bit-wise operations are:
- Bitwise And is just the digit minimum operation in binary.
It can also be thought of as bit-wise "multiplication", since the minimum
of any two bits is equal to the product of those two bits.
- Bitwise Or is just the digit maximum operation in binary.
- Bitwise Exclusive Or is the "are they different" bit-wise
operation. A 1 is placed at the bottom of all columns where the two
bits above are different. A 0 is placed at the bottom of all
other columns. This operation is often abbreviated as "xor".
- Bitwise Complement is the "change them all" operation.
It changes all 1 bits to 0 bits and vice-versa. It is also known as the ones
complement or sometimes just complement. The twos complement of
a number (its signed binary negative) can also be obtained as the ones complement
plus 1.
The three operations can be formed on numbers written in any base by first
converting them to binary, then performing the desired operation, and finally
converting the asnwer back to the original base.
THUS NOTE:
Except for Binary ITSELF,
The binary operations CANNOT be done
in the original number base.
Here are some examples in binary:
11001010 11001010 11001010
01101100 01101100 01101100 11010110
------------- ------------ ------------- ---------------
and: 01001000 or: 11101110 xor: 10100110 cmpl: 00101001
Here are some examples in hexidecimal:
B6 = 1011 0110 B6 = 1011 0110
5C = 0101 1100 5C = 0101 1100
------------------- ------------------
and: 14 = 0001 0100 or: FE = 1111 1110
B6 = 1011 0110
5C = 0101 1100 C7 = 1100 0111
------------------- -------------------
xor: EA = 1110 1010 cmpl: 38 = 0011 1000
Copyright © 1998 Dr. James F. Wirth
Section 5 Multiply
To multiply numbers in some other base, it is strongly suggested that you convert
the numbers to decimal, do the operation in decimal and then convert back to
the original base.
The discussion here assumes that signed binary numbers are being used so you
may need to review the Signed Binary
tutorial.
It is possible to do multiplication directly in base-n, but it would
require you to memorize the n2 elements of the
multiplication tables base-n. For example, in hexidecimal, you would
have to memorize 256 values.
When converting to decimal be careful to convert either signed or unsigned
as specified. The answers can depend on the signs. Also notice that you can
get an answer with twice as many digits as the original two numbers.
On some computers (in particular the ECTC98) the answer is trimmed back to
the original size by dropping the left most digits. This can easily
lead to the wrong answer - a condition known as multiplicative overflow.
Most computers do not automatically detect this error - so be careful!
Here are two examples of eight bit signed multiply
on eight bits quantities
represented in hexidecimal:
Hex Dec Hex Dec
FA = -6 9C = -100
05 = 5 F1 = -15
---------- ----------
E2 = -30 DC = 1500
where the second problem gives the wrong answer. The correct answer should
have been 05DC, but only the right hand eight bits (DC) are kept in the
result.
Section 6 Divide and Mod
Both division and modulus
in base n should actually be done by converting to decimal, performing the desired
arithmetic and then converting the answer back to base n.
The discussion here assumes that signed binary numbers are being used so you
may need to review the Signed Binary
tutorial.
Some details differ:
Division
Modulus
Division (6.1)
Division in base n should actually be done by converting to decimal, performing
the division and then converting the answer back to base n.
Notice that the result of division of base n numbers is another base n integer
- and thus integer
division must be used.
Here is an example in signed base 16 (hexidecimal). Let us divide the
numerator(dividend) FD3B by the denominator(divider) C4.
First we convert to decimal
so that:
FD3B gives: -709
C4 gives: -60
Then we use the calculator to divide -709 by -60:
-709 / -60 = 11.8166666666
Next we truncate, because we must use integer division.
truncating gives: 11
Final we convert back to hexidecimal

converting gives: 0B
Thus the answer in hex is:
FD3B / C4 = 0B
Modulus (6.2)
Modulus in base n should
actually be done by converting to decimal, performing the modulus and then converting
the answer back to base n.
However if any of the operands are negative number, then calculating the modulus
should be done using the formula:
X mod Y = X - (X/Y) * Y
where the quotient (X/Y) must
be the integer division
and where the asterisk * stands for "multiply".
An example
Consider computing the value of:
-103 mod 17
Thus X (see the above formula) is -103
and Y is 17. Thus the
formula becomes:
-103 mod 17 = -103 - (-103/17)
* 17
BUT, the division must be integer division so that
-103/17 = -6
instead of -6.058823.
Thus the formula becomes:
-103 mod 17 = -103 - -6 * 17
and because you do the multiply first (before the subtract)
-103 mod 17 = -103 - -102
= -1
Thus the value of -103
mod 17 is
-1.
USE a calculator if you have trouble doing negative arithmetic. BUT make sure
you use the calculator CORRECTLY.
Leading Zeros
A leading zero is a zero written at the left end of a base-n number
which does not contribute to the value of the number (i.e. its translation
into decimal).
All zeroes at the left end of an unsigned number are leading zeroes.
The zeroes at the left end of a signed binary/hex
number are NOT leading zeros because their presence or absence changes
the decimal value of the number.
As an example where left end zeros are not leading zeros consider
a signed (twos complement) number written in hexidecimal:
- 0B5 whose decimal value is 181.
- Then remove the leftmost zero to get B5
- Since the B is actually binary 1011, then the sign bit is
1 and so the number is negative.
- In fact, translating the signed number B5 into decimal gives -75
which is a completely different answer than 181.
Integer
Division
Most computers (and computer languages such as C and C++) get an integer result
after they divide one integer by another.
This is called integer division
To do an integer division on a calculator:
- First do the indicated division
- You may get a fractional part (after the decimal point)
- Throw the fractional part away (called truncation)
Thus the division 17/5
does not give 3.4 but
rather gives just 3.
On some computers, a negative number is "rounded down" to the next
more negative number. This is called "truncating to negative infinity".
However, on Intel based computers, ordinary truncation is used (just throw
away the fraction).
Thus the problem -17/5
gives these answers:
- -3.4 on a calculator
- -3 on an Intel based
computer
- -4 on a "negative
infinity" computer
It is the negative answer that provides different behavior. Thus the problem
17/5 gives these answers:
- 3.4 on a calculator
- 3 on an Intel based
computer
- 3 on a "negative
infinity" computer
with no difference between the Intel based or "negative infinity"
computer.