23.2 KB · commit a9602da
Integers can be expressed using any integer greater than one as a base, as we will show in this section. Although we commonly use decimal (base 10) representations, binary (base 2), octal (base 8), and hexadecimal (base 16) representations are often used, especially in computer science. Given a base b and an integer n, we will show how to construct the base b representation of this integer. We will also explain how to quickly convert between binary and octal and between binary and hexadecimal notations.
As mentioned in Section 3.1, the term algorithm originally referred to procedures for performing arithmetic operations using the decimal representations of integers. These algorithms, adapted for use with binary representations, are the basis for computer arithmetic. They provide good illustrations of the concept of an algorithm and the complexity of algorithms. For these reasons, they will be discussed in this section.
We will also introduce an algorithm for finding a div d and amodd where a and d are integers with d>1. Finally, we will describe an efficient algorithm for modular exponentiation, which is a particularly important algorithm for cryptography, as we will see in Section 4.6.
In everyday life we use decimal notation to express integers. For example, 965 is used to denote 9⋅102+6⋅10+5. However, it is often convenient to use bases other than 10. In particular, computers usually use binary notation (with 2 as the base) when carrying out arithmetic, and octal (base 8) or hexadecimal (base 16) notation when expressing characters, such as letters or digits. In fact, we can use any integer greater than 1 as the base when expressing integers. This is stated in Theorem 1.
A proof of this theorem can be constructed using mathematical induction, a proof method that is discussed in Section 5.1. 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 (akak−1…a1a0)b. For instance, (245)8 represents 2⋅82+4⋅8+5=165. Typically, the subscript 10 is omitted for base 10 expansions of integers because base 10, or decimal expansions, are commonly used to represent integers.
Choosing 2 as the base gives binary expansions of integers. In binary 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 expansions) are used by computers to represent and do arithmetic with integers.
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. ◀
Among the most important bases in computer science are base 2, base 8, and base 16. Base 8 expansions are called octal expansions and base 16 expansions are hexadecimal expansions.
What is the decimal expansion of the number with octal expansion (7016)8?
Solution: Using the definition of a base b expansion with b=8 tells us that (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).
What is the decimal expansion of the number with hexadecimal expansion (2AE0B)16?
Solution: Using the definition of a base b expansion with b=16 tells us that (2AE0B)16=2⋅164+10⋅163+14⋅162+0⋅16+11=175627. ◀
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.
We will now describe an algorithm for constructing the base b expansion 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 remainders. 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.
Find the octal expansion of (12345)10.
Solution: First, divide 12345 by 8 to obtain 12345=8⋅1543+1. Successively dividing quotients by 8 gives
1543192243=8⋅192+7,=8⋅24+0,=8⋅3+0,=8⋅0+3.The successive remainders that we have found, 1, 7, 0, 0, and 3, are the digits from the right to the left of 12345 in base 8. Hence, (12345)10=(30071)8. ◀
Find the hexadecimal expansion of (177130)10.
Solution: First divide 177130 by 16 to obtain 177130=16⋅11070+10. Successively dividing quotients by 16 gives
11070691432=16⋅691+14,=16⋅43+3,=16⋅2+11,=16⋅0+2.The successive remainders that we have found, 10, 14, 3, 11, 2, give us the digits from the right to the left of 177130 in the hexadecimal (base 16) expansion of (177130)10. It follows that (177130)10=(2B3EA)16. (Recall that the integers 10, 11, and 14 correspond to the hexadecimal digits A,B, and E, respectively.) ◀
Find the binary expansion of (241)10.
Solution: First divide 241 by 2 to obtain 241=2⋅120+1. Successively dividing quotients by 2 gives
120603015731=2⋅60+0,=2⋅30+0,=2⋅15+0,=2⋅7+1,=2⋅3+1,=2⋅1+1,=2⋅0+1.The successive remainders that we have found, 1, 0, 0, 0, 1, 1, 1, 1, are the digits from the right to the left in the binary (base 2) expansion of (241)10. Hence, (241)10=(1111 0001)2. ◀
The pseudocode given in Algorithm 1 finds the base b expansion (ak−1…a1a0)b of the integer n.
| Decimal | Hexadecimal | Octal | Binary |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 10 |
| 3 | 3 | 3 | 11 |
| 4 | 4 | 4 | 100 |
| 5 | 5 | 5 | 101 |
| 6 | 6 | 6 | 110 |
| 7 | 7 | 7 | 111 |
| 8 | 8 | 10 | 1000 |
| 9 | 9 | 11 | 1001 |
| 10 | A | 12 | 1010 |
| 11 | B | 13 | 1011 |
| 12 | C | 14 | 1100 |
| 13 | D | 15 | 1101 |
| 14 | E | 16 | 1110 |
| 15 | F | 17 | 1111 |
In Algorithm 1, q represents the quotient obtained by successive divisions by b, starting with q=n. The digits in the base b expansion are the remainders of these divisions and are given by qmodb. The algorithm terminates when a quotient q=0 is reached.
Conversion between binary and octal and between binary and hexadecimal expansions is extremely easy because each octal digit corresponds to a block of three binary digits and each hexadecimal digit corresponds to a block of four binary digits, with these correspondences shown in Table 1 without initial 0s shown. This conversion is illustrated in 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 digits 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=(11111 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. ◀
The algorithms for performing operations with integers using their binary expansions are extremely important in computer arithmetic. We will describe algorithms for the addition and the multiplication of two integers expressed in binary notation. We will also analyze the computational complexity of these algorithms, in terms of the actual number of bit operations used.
Throughout this discussion, suppose that the binary expansions of a and b are a=(an−1an−2…a1a0)2,b=(bn−1bn−2…b1b0)2, 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.
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=(snsn−1sn−2…s1s0)2.
Add a=(1110)2 and b=(1011)2.
Solution: Following the procedure specified in the algorithm, first note that a0+b0=0+1=0⋅2+1, so that c0=0 and s0=1. Then, because a1+b1+c0=1+1+0=1⋅2+0, it follows that c1=1 and s1=0. Continuing, a2+b2+c1=1+0+1=1⋅2+0, so that c2=1 and s2=0. Finally, because a3+b3+c2=1+1+1=1⋅2+1,
follows that c3=1 and s3=1. This means that s4=c3=1. Therefore, s=a+b=(1 1001)2. This addition is displayed in Figure 1, where carries are shown in blue. ◀
Figure 1 — Adding (1110)2 and (1011)2.
The algorithm for addition can be described using pseudocode as follows.
Next, the number of additions of bits used by Algorithm 2 will be analyzed.
How many additions of bits are required to use Algorithm 2 to add two integers with n bits (or less) in their binary representations?
Solution: Two integers are added by successively adding pairs of bits and, when it occurs, a carry. Adding each pair of bits and the carry requires two additions of bits. Thus, the total number of additions of bits used is less than twice the number of bits in the expansion. Hence, the number of additions of bits used by Algorithm 2 to add two n-bit integers is O(n). ◀
Next, consider the multiplication of two n-bit integers a and b. The conventional algorithm (used when multiplying with pencil and paper) works as follows. Using the distributive law, we see that
ab=a(b020+b121+⋯+bn−12n−1)=a(b020)+a(b121)+⋯+a(bn−12n−1).We can compute ab using this equation. We first note that abj=a if bj=1 and abj=0 if bj=0. Each time we multiply a term by 2, we shift its binary expansion one place to the left and add a zero at the tail end of the expansion. Consequently, we can obtain (abj)2j by shifting the binary expansion of abj j places to the left, adding j zero bits at the tail end of this binary expansion. Finally, we obtain ab by adding the n integers abj2j,j=0,1,2,…,n−1. Algorithm 3 displays this procedure for multiplication.
Example 10 illustrates the use of this algorithm.
Find the product of a=(110)2 and b=(101)2.
Solution: First note that
ab0⋅20ab1⋅21=(110)2⋅1⋅20=(110)2,=(110)2⋅0⋅21=(0000)2,and ab2⋅22=(110)2⋅1⋅22=(11000)2. To find the product, add (110)2,(0000)2, and (11000)2. Carrying out these additions (using Algorithm 2, including initial zero bits when necessary) shows that ab=(1 1110)2. This multiplication is displayed in Figure 2. ◀
Figure 2 — Multiplying (110)2 and (101)2.
Next, we determine the number of additions of bits and shifts of bits used by Algorithm 3 to multiply two integers.
How many additions of bits and shifts of bits are used to multiply a and b using Algorithm 3?
Solution: Algorithm 3 computes the products of a and b by adding the partial products c0,c1,c2,…, and cn−1. When bj=1, we compute the partial product cj by shifting the binary expansion of a by j bits. When bj=0, no shifts are required because cj=0. Hence, to find all n of the integers abj2j,j=0,1,…,n−1, requires at most 0+1+2+⋯+n−1 shifts. Hence, by Example 5 in Section 3.2 the number of shifts required is O(n2).
To add the integers abj from j=0 to j=n−1 requires the addition of an n-bit integer, an (n+1)-bit integer, …, and a (2n)-bit integer. We know from Example 9 that each of these additions requires O(n) additions of bits. Consequently, a total of O(n2) additions of bits are required for all n additions. ◀
Surprisingly, there are more efficient algorithms than the conventional algorithm for multiplying integers. One such algorithm, which uses O(n1.585) bit operations to multiply n-bit numbers, will be described in Section 8.3.
Given integers a and d, d>0, we can find q=a div d and r=amodd using Algorithm 4. In this brute-force algorithm, when a is positive we subtract d from a as many times as necessary until what is left is less than d. The number of times we perform this subtraction is the quotient and what is left over after all these subtractions is the remainder. Algorithm 4 also covers the case where a is negative. This algorithm finds the quotient q and remainder r when ∣a∣ is divided by d. Then, when a<0 and r>0, it uses these to find the quotient −(q+1) and remainder d−r when a is divided by d. Assuming that a>d, this algorithm uses O(qloga) bit operations.
There are more efficient algorithms than Algorithm 4 for determining the quotient q=a div d and the remainder r=amodd when a positive integer a is divided by a positive integer d (see [Kn98] for details). These algorithms require O(loga⋅logd) bit operations. If both of the binary expansions of a and d contain n or fewer bits, then we can replace loga⋅logd by n2. This means that we need O(n2) bit operations to find the quotient and remainder when a is divided by d.
In cryptography it is important to be able to find bnmodm efficiently, where b,n, and m are large integers. It is impractical to first compute bn and then find its remainder when divided by m because bn will be a huge number. Instead, we can use an algorithm that employs the binary expansion of the exponent n.
Before we present this algorithm, we illustrate its basic idea. We will explain how to use the binary expansion of n, say n=(ak−1…a1a0)2, to compute bn. First, note that bn=bak−1⋅2k−1+⋯+a1⋅2+a0=bak−1⋅2k−1…ba1⋅2⋅ba0. This shows that to compute bn, we need only compute the values of b,b2,(b2)2=b4,(b4)2=b8,…,b2k. Once we have these values, we multiply the terms b2j in this list, where aj=1. (For efficiency, after multiplying by each term, we reduce the result modulo m.) This gives us bn. For example, to compute 311 we first note that 11=(1011)2, so that 311=383231. By successively squaring, we find that 32=9,34=92=81, and 38=(81)2=6561. Consequently, 311=383231=6561⋅9⋅3=177,147.
The algorithm successively finds bmodm,b2modm,b4modm,…,b2k−1modm and multiplies together those terms b2jmodm where aj=1, finding the remainder of the product when divided by m after each multiplication. Pseudocode for this algorithm is shown in Algorithm 5.
We illustrate how Algorithm 5 works in Example 12.
Use Algorithm 5 to find 3644mod645.
Solution: Algorithm 5 initially sets x=1 and power=3mod645=3. In the computation of 3644mod645, this algorithm determines 32jmod645 for j=1,2,…,9 by successively squaring and reducing modulo 645. If aj=1 (where aj is the bit in the jth position in the binary expansion of 644, which is (1010000100)2), it multiplies the current value of x by 32jmod645 and reduces the result modulo 645. Here are the steps used:
This shows that following the steps of Algorithm 5 produces the result 3644mod645=36. ◀
Algorithm 5 is quite efficient; it uses O((logm)2logn) bit operations to find bnmodm.