68. Computing integers to different bases

68.1 Representing an integer

68.1.1 Remark Given a nonnegative integer $n$ and a base $b$, the most significant nonzero digit of $n$ when it is represented in base $b$ is the quotient when $n$ is divided by the largest power of $b$ less than $n$. For example, in base 10, the most significant digit of 568 is 5, and indeed $5 = 568 \operatorname{div} 100$ (100 is the largest power of 10 less than 568). Furthermore, 68 is the remainder when 568 is divided by 500.

This observation provides a way of computing the base-$b$ representation of an integer.

68.1.2 Method

Suppose the representation for $n$ to base $b$ is ‘$d_{m}d_{m-1}\cdots d_{0}$’, where $d_{i}$ represents the integer $n_{i}$ in base $b$. Then

$$d_{m} = n \operatorname{div} b^{m}$$

and

$$d_{m-1} = (n-d_{m}b^{m}) \operatorname{div} b^{m-1}$$

In general, for all $i = 0, 1,\ldots ,m-1$,

$$d_{i} = (n_{i+1}-d_{i+1}b^{i+1}) \operatorname{div} b^{i} \tag{68.1}$$

where

$$n_{m} = n \tag{68.2}$$

and for $i = 0, 1,\ldots ,m-1$,

$$n_{i} = n_{i+1} - d_{i+1}b^{i+1} \tag{68.3}$$

68.1.3 Example The ‘6’ in 568 is

$$(568 - 5\cdot 100) \operatorname{div} 10$$

(here $m = 2$: note that the 5 in 568 is $d_{2}$ since we start counting on the right at 0).

68.1.4 Remark Observe that (68.1) can be written

$$d_{i} = (n \bmod b^{i+1}) \operatorname{div} b^{i} \tag{68.4}$$

which is correct for all $i = 0, 1,\ldots ,m$. The way (68.1) is written shows that the computation of $n \bmod b^{i+1}$ uses the previously-calculated digit $d_{i+1}$.

68.1.5 Example We illustrate this process by determining the representation of 775 to base 8. Note that $512 = 8^{3}$:

a) $775 \operatorname{div} 512 = 1$.

b) $775 - 1\times 512 = 263$.

c) $263 \operatorname{div} 64 = 4$.

d) $263 - 4\times 64 = 7$.

e) $7 \operatorname{div} 8 = 0$.

f) $7 - 0\times 8 = 7$.

g) $7 \operatorname{div} 1 = 7$. And 775 in octal is indeed 1407.

68.2 The algorithm in Pascal

The algorithm just described is expressed in Pascal in Program 68.1. This algorithm is perhaps the most efficient for pencil-and-paper computation. As given, it only works as written for bases up to and including 10; to have it print out ‘a’ for 11, ‘b’ for 12 and so on would require modifying the “write(place)” statement.

var N, base, count, power, limit, place: integer;
(* Requires B > 0 and base > 1 *)
   begin
      power := 1; limit := N div base;
      (*calculate the highest power of the base less than N*)
        while power <= limit do
          begin
            power := power*base
          end;
        while power > 1 do
          begin
            place := N div power; write(place);
            n := n-place*power; power := power div base
          end
   end

Program 68.1: Program for Base Conversion

68.3 Another base conversion algorithm

Another algorithm, which computes the digits backwards, stores them in an array, and then prints them out in the correct order, is given in Program 68.2. It is more efficient because it is unnecessary to calculate the highest power of the base less than $N$ first. This program starts with the observation that the least significant digit in a number $n$ expressed in base $b$ notation is $n \bmod b$. The other digits in the representation of $n$ represent $(n - (n \bmod b))/b$. For example, $568 \bmod 10 = 8$, and the number represented by the other digits, 56, is $(568 - 8)/10$.

In the program in Program 68.2, count and u are auxiliary variables of type integer. The size longest of the array D has to be known in advance, so there is a bound on the size of integer this program can compute, in contrast to the previous algorithm. It is instructive to carry out the operations of the program in Program 68.2 by hand to see how it works.

var count, u, N, base: integer;
var D:array [0..longest] of integer;
  begin
    count := 0; u := N;
    while u<>0 do
      begin
        D[count] := u mod base;
        u := (u-D[count]) div base;
        count := count+1
      end;
    while count<>0 do
      begin
        count := count-1;
        write D[count]
      end
  end;

Program 68.2: Faster Program for Base Conversion

68.4 Comments on the notation for integers

Suppose $n$ is written ‘$d_{m}d_{m-1}\ldots d_{0}$’ in base $b$. Then the exact significance of $d_{m}$, namely the power $b^{m}$ that its value $n_{m}$ is multiplied by in Equation (66.2) of Definition 66.4 (page 94), depends on the length of the string of digits representing $n$ (the length is $m+1$ because the count starts at 0). If you read the digits from left to right, as is usual in English, you have to read to the end before you know what $m$ is. On the other hand, the significance of the right digit $d_{0}$ is known without knowing the length $m$. In particular, the program in Program 68.1 has to read to the end of the representation to know the power $b^{m}$ to start with.

The fact that the significance of a digit is determined by its distance from the right is the reason a column of integers you want to add is always lined up with the right side straight. In contrast to this, the sentences on a typewritten page are lined up with the left margin straight.

There is a good reason for this state of affairs: this notation was invented by Arab mathematicians, and Arabic is written from right to left.

68.4.1 Exercise Represent the numbers 100, 111, 127 and 128 in binary, octal, hexadecimal and base 36. (Answer on page 246.)

68.4.2 Exercise Represent the numbers 3501, 29398 and 602346 in hexadecimal and base 36.

68.5 Exercise set

Exercises 68.5.1 through 68.5.4 are designed to give a proof of Formula (68.4), page 99, so they should be carried out without using facts about how numbers are represented in base $b$. In these exercises, all the variables are of type integer.

68.5.1 Exercise Let $b > 1$. Prove that if for all $i \geq 0$, $0\leq d_{i} < b$, then

$$d_{m}b^{m} + d_{m-1}b^{m-1} + \cdots + d_{1}b + d_{0} < b^{m+1}$$

68.5.2 Exercise Let $b > 1$ and $n > 0$. Let $n = d_{m}b^{m} + \cdots + d_{1}b + d_{0}$ with $0\leq d_{i} < b$ for $i = 0, 1,\ldots ,m$. Prove that for any $i\geq 0$,

$$n = b^{i}[ d_{m}b^{m-i} + d_{m-1}b^{m-i-1} + \cdots + d_{i}] + d_{i-1}b^{i-1} + \cdots + d_{1}b + d_{0}$$

and $0\leq d_{i-1}b^{i-1} + \cdots + d_{1}b + d_{0} < b^{i}$. (Hint: Use Exercise 68.5.1.)

68.5.3 Exercise Let $b > 1$ and $n > 0$ and let $n = d_{m}b^{m} + \cdots + d_{1}b + d_{0}$ with $0\leq d_{i} < b$ for $i = 0, 1,\ldots ,m$. Prove that for any $i \geq 0$,

$$d_{m}b^{m-i} + d_{m-1}b^{m-i-1} + \cdots + d_{i} = n \operatorname{div} b^{i}$$

and

$$d_{i}b^{i} + \cdots + d_{1}b + d_{0} = n \bmod b^{i+1}$$

68.5.4 Exercise Prove Equation (68.4), page 99.

Pages 99–102 of the book (PDF).