67. Algorithms and bases
Among the first algorithms of any complexity that most people learn as children are the algorithms for adding, subtracting, multiplying and dividing integers written in decimal notation. In medieval times, the word “algorithm” referred specifically to these processes.
67.1 Addition
The usual algorithm for addition you learned in grade school works for numbers in other bases than 10 as well. The only difference is that you have to use a different addition table for the digits.
67.1.1 Example To add 95a and b87 in hexadecimal you write them one above the other:
95a
+b87
----
14e1
Here is a detailed description of how this is done, all in base 16.
- Calculate $\mathrm{a} + 7 = 11_{16}$, with a carry of 1 since $11_{16} \geq 10_{16}$. (Pronounce $10_{16}$ as “one-zero”, not “ten”, since it denotes sixteen, and similarly for $11_{16}$ which denotes seventeen. By the way, the easiest way to figure out what a + 7 is is to count on your fingers!)
- Then add 5 and 8 and get d (not 13!) and the carry makes e. $\mathrm{e} < 10_{16}$ so there is no carry.
- Finally, $9 + \mathrm{b} = 14_{16}$.
So the answer is $14\mathrm{e}1_{16}$. The whole process is carried out in hexadecimal without any conversion to decimal notation.
67.1.2 Addition in binary The addition table for binary notation is especially simple: $0 + 0 = 0$ without carry, $1 + 0 = 0 + 1 = 1$ without carry, and $1 + 1 = 0$ with carry.
67.2 Multiplication
The multiplication algorithm similarly carries over to other bases. Normally in a multiplication like
346 (multiplicand)
×527 (multiplier)
----
2422
6920 (partial products)
173000
------
182342 (product)
you produce successive partial products, and then you add them. The partial product resulting from multiplying by the $i$th digit of the multiplier is
$$\text{digit} \times \text{multiplicand} \times 10^{i}$$
(Most people are taught in grade school to suppress the zeroes to the right of the multiplying digit.)
67.2.1 Binary multiplication Multiplication in binary has a drastic simplification. In binary notation, the only digits are 0, which causes a missing line, and 1, which involves only shifting the top number. So multiplying one number by another in binary consists merely of shifting the first number once for each 1 in the second number and adding.
67.2.2 Example With trailing zeroes suppressed:
1101
×1101
1101
1101
1101
--------
10101001
67.2.3 Exercise Perform these additions and multiplications in binary.
a)
110001
+101111
b)
1011101
+1110101
c)
10011
×10101
d)
11100
×11001
(Answer on page 246.)
67.2.4 Exercise Perform these additions in hexadecimal:
a)
9ae
+b77
b)
389
+777
c)
feed
+dad
(Answer on page 246.)
67.2.5 Exercise Show that in adding two numbers in base $b$, the carry is never more than 1, and in multiplying in base $b$, the carry is never more than $b-2$.
67.2.6 Exercise (discussion) Because subtracting two numbers using pencil and paper is essentially a solitary endeavor, most people are not aware that there are two different algorithms taught in different public school systems. Most American states’ school systems teach one algorithm (Georgia used to be an exception),and many European countries teach another one. Ask friends from different parts of the world to subtract 365 from 723 while you watch, explaining each step, and see if you detect anyone doing it differently from the way you do it.
Pages 97–98 of the book (PDF).