66. Bases for representing integers

66.1 Characters and strings

The number of states in the United States of America is an integer. In the usual notation, that integer is written ‘50’.

In this section, we discuss other, related ways of expressing integers which are useful in applications to computer science. In doing this it is important to distinguish between numerals like ‘5’ and ‘0’ and the integers they represent. In particular, the sequence of numerals ‘50’ represents the integer which is the number of states in the USA, but it is not the same thing as that integer.

Numerals, as well as letters of the alphabet and punctuation marks, are characters. Characters are a type of data, distinct from integers or other numerical types. In order to distinguish between a character like ‘5’ and the number 5 we put characters which we are discussing in single quotes. Pascal has a data type CHAR of which numerals and letters of the alphabet are subtypes. Single quotes are used in Pascal as we use them.

66.2 Specification: string

A sequence of characters, such as ‘50’ or ‘cat’, is also a type of data called a string.

66.2.1 Remarks

a) Strings will be discussed from a theoretical point of view in Chapter 109.

b) In this book we put strings in single quotes when we discuss them. Thus ‘cat’ is a string of characters whereas “cat” is an English word (and a cat is an animal!).

66.3 Bases

The decimal notation we usually use expresses an integer as a string formed of the numerals ‘0’, ‘1’, …, ‘9’. These numerals are the decimal digits. The word “digit” is often used for the integers they represent, as well. The notation is based on the fact that any positive integer can be expressed as a sum of numbers, each of which is the value of a digit times a power of ten. Thus

$$258 = 2\times 10^{2} + 5\times 10^{1} + 8\times 10^{0}.$$

The expression ‘258’ gives you the digits multiplying each power of 10 in decreasing order, the rightmost numeral giving the digit which multiplies $1 = 10^{0}$.

Any integer greater than 1 can be used instead of 10 in an analogous way to express integers. The integer which is used is the base or radix of the notation. In octal notation, for example, the base is 8, and the octal digits are ‘0’, ‘1’, …, ‘7’. For example,

$$258 = 4\times 8^{2} + 0\times 8^{1} + 2\times 8^{0}$$

so the number represented by ‘258’ in decimal notation is represented in octal notation by ‘402’.

Here is the general definition for the representation of an integer in base $b$.

66.4 Definition: base

If $n$ and $b$ are nonnegative integers and $b > 1$, then the expression

$$\text{‘}d_{m}d_{m-1}d_{m-2}\cdots d_{1}d_{0}\text{’} \tag{66.1}$$

represents $n$ in base $b$ notation if for each $i$, $d_{i}$ is a symbol (base-$b$ digit) representing the integer $n_{i}$,

$$n = n_{m}b^{m} + n_{m-1}b^{m-1} + \cdots + n_{0}b^{0} \tag{66.2}$$

and for all $i$,

$$0\leq n_{i} \leq b-1 \tag{66.3}$$

66.4.1 Remarks

a) We will say more about the symbols $d_{i}$ below. For bases $b \leq 10$ these symbols are normally the usual decimal digits,

$$d_{0} = \text{‘0’},\ d_{1} = \text{‘1’},\ \ldots ,d_{9} = \text{‘9’}$$

as illustrated in the preceding discussion.

b) Efficient ways of determining the base-$b$ representation of some integer are discussed in Chapter 68. Note that you can do the exercises in this section without knowing how to find the base-$b$ representation of an integer — all you need to know is its definition.

66.4.2 Notation When necessary, we will use the base as a subscript to make it clear which base is being used. Thus $258_{10} = 402_{8}$, meaning that the number represented by ‘258’ in base 10 is represented by ‘402’ in base 8.

66.5 Definition: significance

The digit $d_{i}$ is more significant than $d_{j}$ if $i > j$. Thus, if a number $n$ is represented by ‘$d_{m}d_{m-1}\ldots d_{1}d_{0}$’, then $d_{0}$ is the least significant digit and, if $d_{m}$ does not denote 0, it is the most significant digit.

66.5.1 Example The least significant digit in $258_{10}$ is 8 and the most significant is 2.

66.5.2 Remark For a given $b$ and $n$, the following theorem says that the representation given by definition 66.4 is unique, except for the choice of the symbols representing the $n_{i}$. We will take this theorem as known.

66.6 Theorem

If $n$ and $b$ are positive integers with $b > 1$, then there is only one sequence $n_{0},n_{1},\ldots ,n_{m}$ of integers for which $n_{m} \neq 0$ and formulas (66.2) and (66.3) are true.

66.6.1 Worked Exercise Prove that the base 4 representation of 365 is 11231. Answer $365 = 1\cdot 4^{4} + 1\cdot 4^{3} + 2\cdot 4^{2} + 3\cdot 4^{1} + 1\cdot 4^{0}$, and $1,2,3$ are all less than 4, so the result follows from Theorem 66.6.

Note that in this answer we merely showed that 11231 fit the definition. That is all that is necessary. Of course, if you are not given the digits as you were in this problem, you need some way of calculating them. We will describe ways of doing that in Chapter 68.

66.6.2 Exercise Prove that the base 8 representation of 365 is 555.

66.6.3 Exercise Prove that if an integer $n$ is represented by ‘$d_{m}d_{m-1}\cdots d_{1}$’ in base $b$, then ‘$d_{m}d_{m-1}\cdots d_{1}0$’ represent $bn$ in base $b$ notation. (Answer on page 246.)

66.6.4 Exercise Suppose $b$ is an integer greater than 1 and suppose $n$ is an integer such that the base $b$ representation of $n$ is 352. Prove using only the definition of representation to base $b$ that the base $b$ representation of $b^{2}n + 1$ is 35201.

66.7 Specific bases

66.7.1 Base 2 The digits for base 2 are ‘0’ and ‘1’ and are called bits. Base 2 notation is called binary notation.

66.7.2 Bases larger than 10 For bases $b \leq 10$, the usual numerals are used, as mentioned before. A problem arises for bases bigger than 10: you need single symbols for the integers $10, 11, \ldots$. Standard practice is to use the letters of the alphabet (lowercase here, uppercase in many texts): ‘a’ denotes 10, ‘b’ denotes 11, and so on. This allows bases up through 36.

66.7.3 Base 16 Base 16 (giving hexadecimal notation) is very commonly used in computing. For example, $95_{10}$ is $5\mathrm{f}_{16}$, and $266_{10}$ is hexadecimal $10\mathrm{a}_{16}$ (read this “one zero a”, not “ten a”!) In texts in which decimal and nondecimal bases are mixed, the numbers expressed nondecimally are often preceded or followed by some symbol; for example, many authors write \$10a or H10a to indicate $266_{10}$ expressed hexadecimally.

66.8 About representations

(This continues the discussion of representations in Section 10.2 and Remark 17.1.3.) It is important to distinguish between the (abstract) integer and any representation of it. The number of states in the U.S.A is represented as ‘50’ in decimal notation, as ‘110010’ in binary, and as a pattern of electrical charges in in a computer. These are all representations or realizations of the abstract integer. (The word “realization” here has a technical meaning, roughly made real or made concrete.) All the representations are matters of convention, in other words, are based on agreement rather than intrinsic properties. Moreover, no one representation is more fundamental or correct than another, although one may be more familiar or more convenient than another.

There is also a distinction to be made between properties of an integer and properties of the representation of an integer. For example, being prime is a property of the integer; whether it is written in decimal or binary is irrelevant. Whether its least significant digit is 0, on the other hand, is a property of the representation: the number of states in the USA written in base 10 ends in ‘0’, but in base 3 it ends in ‘2’.

66.8.1 Exercise Suppose $b$ is an integer greater than 1, $a$ is an integer dividing $b$, and $n$ is an integer. When $n$ is written in base $b$, how do you tell from the digits of $n$ whether $n$ is divisible by $a$? Prove that your answer is correct.

66.8.2 Exercise Would Theorem 66.6 still be true if the requirement that $0\leq n_{i} \leq b-1$ for all $i$ were replaced by the requirement that the $n_{i}$ be nonnegative?

66.8.3 Exercise (Mathematica) A positive integer is a repunit if all its decimal digits are 1.

a) Use Mathematica to determine which of the repunits up to a billion are divisible by 3.

b) Based on the results of part (a), formulate a conjecture as to which repunits are divisible by 3. The conjecture should apply to all repunits, not just those less than a billion.

c) Prove the conjecture.

66.8.4 Exercise (discussion) Some computer languages (FORTH is an example) have a built-in integer variable BASE. Whatever integer you set BASE to will be used as the base for all numbers output. How would you discover the current value of BASE in such a language? (Assume you print the value of a variable X by writing PRINT(X)).

Pages 93–96 of the book (PDF).