Integer Representations and Algorithms: 1. Finally, We Will Describe An e Cient Algorithm For Modular Exponentiation
Integer Representations and Algorithms: 1. Finally, We Will Describe An e Cient Algorithm For Modular Exponentiation
THEOREM 1 Let b be an integer greater than 1. Then if n is a positive integer, it can be expressed uniquely
in the form
n = ak bk + ak−1 bk−1 + ⋯ + a1 b + a0 ,
A proof of this theorem can be constructed using mathematical induction, a proof method that
is discussed in Section 5.1. It can also be found in [Ro10]. The representation of n given in
Theorem 1 is called the base b expansion of n. The base b expansion of n is denoted by
(ak ak−1 … a1 a0 )b . For instance, (245)8 represents 2 ⋅ 82 + 4 ⋅ 8 + 5 = 165. Typically, the sub-
script 10 is omitted for base 10 expansions of integers because base 10, or decimal expansions,
are commonly used to represent integers.
BINARY EXPANSIONS Choosing 2 as the base gives binary expansions of integers. In bi-
nary notation each digit is either a 0 or a 1. In other words, the binary expansion of an integer
is just a bit string. Binary expansions (and related expansions that are variants of binary expan-
sions) are used by computers to represent and do arithmetic with integers.
4.2 Integer Representations and Algorithms 261
EXAMPLE 1 What is the decimal expansion of the integer that has (1 0101 1111)2 as its binary expansion?
Solution: We have
(1 0101 1111)2 = 1 ⋅ 28 + 0 ⋅ 27 + 1 ⋅ 26 + 0 ⋅ 25 + 1 ⋅ 24
+ 1 ⋅ 23 + 1 ⋅ 22 + 1 ⋅ 21 + 1 ⋅ 20 = 351. ◂
OCTAL AND HEXADECIMAL EXPANSIONS Among the most important bases in com-
puter science are base 2, base 8, and base 16. Base 8 expansions are called octal expansions and
base 16 expansions are hexadecimal expansions.
EXAMPLE 2 What is the decimal expansion of the number with octal expansion (7016)8 ?
(7016)8 = 7 ⋅ 83 + 0 ⋅ 82 + 1 ⋅ 8 + 6 = 3598. ◂
Sixteen different digits are required for hexadecimal expansions. Usually, the hexadecimal
digits used are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F, where the letters A through F
represent the digits corresponding to the numbers 10 through 15 (in decimal notation).
EXAMPLE 3 What is the decimal expansion of the number with hexadecimal expansion (2AE0B)16 ?
Each hexadecimal digit can be represented using four bits. For instance, we see that
(1110 0101)2 = (E5)16 because (1110)2 = (E)16 and (0101)2 = (5)16 . Bytes, which are bit
strings of length eight, can be represented by two hexadecimal digits.
BASE CONVERSION We will now describe an algorithm for constructing the base b expan-
sion of an integer n. First, divide n by b to obtain a quotient and remainder, that is,
n = bq0 + a0 , 0 ≤ a0 < b.
The remainder, a0 , is the rightmost digit in the base b expansion of n. Next, divide q0 by b to
obtain
q0 = bq1 + a1 , 0 ≤ a1 < b.
We see that a1 is the second digit from the right in the base b expansion of n. Continue this
process, successively dividing the quotients by b, obtaining additional base b digits as the re-
mainders. This process terminates when we obtain a quotient equal to zero. It produces the base
b digits of n from the right to the left.
The pseudocode given in Algorithm 1 finds the base b expansion (ak−1 … a1 a0 )b of the
integer n.
4.2 Integer Representations and Algorithms 263
TABLE 1 Hexadecimal, Octal, and Binary Representation of the Integers 0 through 15.
Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F
Octal 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17
Binary 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111
Remark: Note that Algorithm 1 can be thought of as a greedy algorithm, because the base b
digits are taken as large as possible in each step.
EXAMPLE 7 Find the octal and hexadecimal expansions of (11 1110 1011 1100)2 and the binary expansions
of (765)8 and (A8D)16 .
Solution: To convert (11 1110 1011 1100)2 into octal notation we group the binary dig-
its into blocks of three, adding initial zeros at the start of the leftmost block if necessary.
These blocks, from left to right, are 011, 111, 010, 111, and 100, corresponding to 3, 7, 2, 7,
and 4, respectively. Consequently, (11 1110 1011 1100)2 = (37274)8 . To convert (11 1110 1011
1100)2 into hexadecimal notation we group the binary digits into blocks of four, adding initial
zeros at the start of the leftmost block if necessary. These blocks, from left to right, are 0011,
1110, 1011, and 1100, corresponding to the hexadecimal digits 3, E, B, and C, respectively.
Consequently, (11 1110 1011 1100)2 = (3EBC)16 .
To convert (765)8 into binary notation, we replace each octal digit by a block of three binary
digits. These blocks are 111, 110, and 101. Hence, (765)8 = (1 1111 0101)2 . To convert (A8D)16
into binary notation, we replace each hexadecimal digit by a block of four binary digits. These
blocks are 1010, 1000, and 1101. Hence, (A8D)16 = (1010 1000 1101)2 . ◂
264 4 / Number Theory and Cryptography
so that a and b each have n bits (putting bits equal to 0 at the beginning of one of these expansions
if necessary).
We will measure the complexity of algorithms for integer arithmetic in terms of the number
of bits in these numbers.
ADDITION ALGORITHM Consider the problem of adding two integers in binary notation.
A procedure to perform addition can be based on the usual method for adding numbers with
pencil and paper. This method proceeds by adding pairs of binary digits together with carries,
when they occur, to compute the sum of two integers. This procedure will now be specified
in detail.
To add a and b, first add their rightmost bits. This gives
a0 + b0 = c0 ⋅ 2 + s0 ,
where s0 is the rightmost bit in the binary expansion of a + b and c0 is the carry, which is either
0 or 1. Then add the next pair of bits and the carry,
a1 + b1 + c0 = c1 ⋅ 2 + s1 ,
where s1 is the next bit (from the right) in the binary expansion of a + b, and c1 is the carry.
Continue this process, adding the corresponding bits in the two binary expansions and the carry,
to determine the next bit from the right in the binary expansion of a + b. At the last stage,
add an−1 , bn−1 , and cn−2 to obtain cn−1 ⋅ 2 + sn−1 . The leading bit of the sum is sn = cn−1 . This
procedure produces the binary expansion of the sum, namely, a + b = (sn sn−1 sn−2 … s1 s0 )2 .
Solution: Following the procedure specified in the algorithm, first note that
a0 + b0 = 0 + 1 = 0 ⋅ 2 + 1,
a1 + b1 + c0 = 1 + 1 + 0 = 1 ⋅ 2 + 0,
a2 + b2 + c1 = 1 + 0 + 1 = 1 ⋅ 2 + 0,