• Tidak ada hasil yang ditemukan

Strings and Number Systems

109

110

use other number systems, such as octal (base eight) and hexadecimal (base 16) as short- hand for these numbers.

To identify the system being used, you attach the base as a subscript to the number. For example, the following numbers represent the quantity 41510 in the binary, octal, decimal, and hexadecimal systems:

415 in binary notation 1100111112

415 in octal notation 6378

415 in decimal notation 41510

415 in hexadecimal notation 19F16

The digits used in each system are counted from 0 to n – 1, where n is the system’s base.

Thus, the digits 8 and 9 do not appear in the octal system. To represent digits with values larger than 910, systems such as base 16 use letters. Thus, A16 represents the quantity 1010, whereas 1016 represents the quantity 1610. In this section, we examine how these systems represent numeric quantities and how to translate from one notation to another.

The Positional System for Representing Numbers

All of the number systems we have examined use positional notation—that is, the value of each digit in a number is determined by the digit’s position in the number. In other words, each digit has a positional value. The positional value of a digit is determined by raising the base of the system to the power specified by the position (baseposition). For an n-digit number, the positions (and exponents) are numbered from n – 1 down to 0, starting with the leftmost digit and mov- ing to the right. For example, as Figure 4-3 illustrates, the positional values of the three-digit number 41510 are 100 (10 )2 , 10 (10 )1 , and 1 (10 )0 , moving from left to right in the number.

To determine the quantity represented by a number in any system from base 2 through base 10, you multiply each digit (as a decimal number) by its positional value and add the results.

The following example shows how this is done for a three-digit number in base 10:

415105

∗ 1 ∗ 1 ∗ 5

4 102 1 101 5 100 4 100 1 10∗ 1 ∗ 15 1∗ 5

400 +10 +5 5415

Figure 4-3 The first three positional values in the base-10 number system Positional values 100 10 1

Positions 2 1 0

Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203

111 Strings and Number Systems

Converting Binary to Decimal

Like the decimal system, the binary system also uses positional notation. However, each digit or bit in a binary number has a positional value that is a power of 2. In the discussion that follows, we occasionally refer to a binary number as a string of bits or a bit string. You determine the integer quantity that a string of bits represents in the usual manner: Multiply the value of each bit (0 or 1) by its positional value and add the results. Let’s do that for the number 11001112:

110011125

∗ 1 ∗ 1 ∗ 1 ∗ + ∗ + ∗ + ∗ 5 1 26 1 25 0 24 0 23 1 22 1 21 1 20 1 64 1 32 0 16 0 8 1 4 1 2 1 1∗ + ∗ + ∗ + ∗ + ∗ + ∗ + ∗ 5

64 +32 +4 +2 +1 5103

Not only have we determined the integer value of this binary number, but we have also converted it to decimal in the process! In computing the value of a binary number, we can ignore the values of the positions occupied by 0s and simply add the positional values of the positions occupied by 1s.

We can code an algorithm for the conversion of a binary number to the equivalent decimal number as a Python script. The input to the script is a string of bits, and its output is the integer that the string represents. The algorithm uses a loop that accumulates the sum of a set of integers. The sum is initially 0. The exponent that corresponds to the position of the string’s leftmost bit is the length of the bit string minus 1. The loop visits the digits in the string from the first to the last (left to right), but counts from the largest exponent of 2 down to 0 as it goes. Each digit is converted to its integer value (1 or 0), multiplied by its positional value, and the result is added to the ongoing total. A positional value is computed by using the ** operator. Here is the code for the script, followed by some example sessions in the shell:

"""

File: binarytodecimal.py

Converts a string of bits to a decimal integer.

"""

bitString = input("Enter a string of bits: ") decimal = 0

exponent = len(bitString) - 1 for digit in bitString:

decimal = decimal + int(digit) * 2 ** exponent exponent = exponent - 1

print("The integer value is", decimal)  

 

Enter a string of bits: 1111 The integer value is 15  

Enter a string of bits: 101 The integer value is 5

Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203

112

Converting Decimal to Binary

How are integers converted from decimal to binary? One algorithm uses division and subtraction instead of multiplication and addition. This algorithm repeatedly divides the decimal number by 2. After each division, the remainder (either a 0 or a 1) is placed at the beginning of a string of bits. The quotient becomes the next dividend in the process.

The string of bits is initially empty, and the process continues while the decimal number is greater than 0.

Let’s code this algorithm as a Python script and run it to display the intermediate results in the process. The script expects a non-negative decimal integer as an input and prints the equivalent bit string. The script checks first for a 0 and prints the string '0' as a special case. Otherwise, the script uses the algorithm just described. On each pass through the loop, the values of the quotient, remainder, and result string are displayed. Here is the code for the script, followed by a session to convert the number 34:

"""

File: decimaltobinary.py

Converts a decimal integer to a string of bits.

"""

 

decimal = int(input("Enter a decimal integer: ")) if decimal == 0:

print(0) else:

print("Quotient Remainder Binary") bitString = ""

while decimal > 0:

remainder = decimal % 2 decimal = decimal // 2

bitString = str(remainder) + bitString print("%5d%8d%12s" % (decimal, remainder, bitString))

print("The binary representation is", bitString) Enter a decimal integer: 34

Quotient Remainder Binary 17 0 0 8 1 10 4 0 010 2 0 0010 1 0 00010 0 1 100010

The binary representation is 100010

Conversion Shortcuts

There are various shortcuts for determining the decimal integer values of some binary numbers. One useful method involves learning to count through the numbers correspond- ing to the decimal values 0 through 8, as shown in Table 4-1.Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203

113 Strings and Number Systems

Note the rows that contain exact powers of 2 (2, 4, and 8 in decimal). Each of the cor- responding binary numbers in that row contains a 1 followed by a number of zeroes that equal the exponent used to compute that power of 2. Thus, a quick way to compute the decimal value of the number 100002 is 24 or 1610.

The rows whose binary numbers contain all 1s correspond to decimal numbers that are one less than the next exact power of 2. For example, the number 1112 equals 2321, or 710. Thus, a quick way to compute the decimal value of the number 111112 is 2521, or 3110.

Octal and Hexadecimal Numbers

The octal system uses a base of eight and the digits 0 . . . 7. Conversions of octal to decimal and decimal to octal use algorithms similar to those discussed thus far (using powers of 8 and multiplying or dividing by 8, instead of 2). But the real benefit of the octal system is the ease of converting octal numbers to and from binary. With practice, you can learn to do these conversions quite easily by hand, and in many cases by eye. To convert from octal to binary, you start by assuming that each digit in the octal number represents three digits in the corresponding binary number. You then start with the leftmost octal digit and write down the corresponding binary digits, padding these to the left with 0s to the count of 3, if necessary. You proceed in this manner until you have converted all of the octal digits.

Figure 4-4 shows such a conversion:

To convert binary to octal, you begin at the right and factor the bits into groups of three bits each. You then convert each group of three bits to the octal digit they represent.

As the size of a number system’s base increases, so does the system’s expressive power, its ability to say more with less. As bit strings get longer, the octal system becomes a less useful

Decimal Binary

0 0

1 1

2 10

3 11

4 100

5 101

6 110

7 111

8 1000

table 4-1 The numbers 0 through 8 in binary

Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203

114

shorthand for expressing them. The hexadecimal or base-16 system (called “hex” for short), which uses 16 different digits, provides a more concise notation than octal for larger numbers. Base 16 uses the digits 0 . . . 9 for the corresponding integer quantities and the letters A . . . F for the integer quantities 10 . . . 15.

The conversion between numbers in the two systems works as follows. Each digit in the hexadecimal number is equivalent to four digits in the binary number. Thus, to convert from hexadecimal to binary, you replace each hexadecimal digit with the cor- responding 4-bit binary number. To convert from binary to hexadecimal, you factor the bits into groups of four and look up the corresponding hex digits. (This is the kind of stuff that hackers memorize). Figure 4-5 shows a mapping of hexadecimal digits to binary digits.

Figure 4-4 The conversion of octal to binary Octal

Binary

437

100 011111

Figure 4-5 The conversion of hexadecimal to binary Hexadecimal

Binary

43F

0100 00111111

exercises

1. Translate each of the following numbers to decimal numbers:

a. 110012

b. 1000002

c. 111112

2. Translate each of the following numbers to binary numbers:

a. 4710

b. 12710

c. 6410

Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-203

115

Dokumen terkait