Working with Numbering Systems

By Larry Long

Learning Objectives  To understand the principles of numbering systems  To convert numbers from one base to another  To be able to do arithmetic in binary and hexadecimal

This supplement to Long and Long learning resources (published by Prentice Hall) on computer competency presents the principles of numbering systems, discusses numbering-system arithmetic, and illustrates how to convert a value in one numbering system to its equivalent in another. After studying this supplement you will be able to perform rudimentary arithmetic operations in the binary and hexadecimal numbering systems.

CONTENTS Numbering Systems and Computers Principles of Numbering Systems Converting Numbers from One Base into Another Arithmetic in Binary and Hexadecimal Examples of Binary Arithmetic Examples of Hexadecimal Arithmetic Self - Check with Answers NUMBERING SYSTEMS AND COMPUTERS We humans use a decimal, or base-10, numbering system, presumably because people have 10 fingers. If we had three fingers and a thumb on each hand, as does the Extra Terrestrial (E.T.), from the classic movie, and Homer Simpson of the Simpson’s, then in all probability we would be using the octal numbering system, which has a base of 8. Early computers were designed around the decimal numbering system. This approach made the creation of computer logic capabilities unnecessarily complex and did not make efficient use of resources. (For example, 10 vacuum tubes were needed to represent one decimal digit.) In 1945, as computer pioneers were struggling to improve this cumbersome approach, John von Neumann suggested that the numbering system used by computers should take advantage of the physical characteristics of electronic circuitry. To deal with the basic electronic states of on and off, von Neumann suggested using the binary numbering system. His insight has vastly simplified the way computers handle data. Computers operate in binary and communicate to us in decimal. A special program translates decimal into binary on input, and binary into decimal on output. Under normal circumstances, a programmer would see only decimal input and output. On occasion, though, he or she must deal with long and confusing strings of 1s and 0s that represent the content of RAM, the computers memory. Occasionally a programmer or computer engineer takes a snapshot of the contents of RAM (on-bits and off-bits) at a given moment in time. To reduce at least part of the confusion of seeing only 1s and 0s on the output, the hexadecimal (base-16) numbering system is used as a shorthand to display the binary contents of both RAM and secondary storage, such as disk. The decimal equivalents for binary, decimal, and hexadecimal numbers are shown in Figure 1. We know that in decimal, any number greater than 9 is represented by a sequence of digits. When you count in decimal, you "carry" to the next position in groups of 10. As you examine Figure 1, notice that you carry in groups of 2 in binary and in groups of 16 in hexadecimal. Also note that any combination of four binary digits can be represented by one "hex" digit.

FIGURE 1 Numbering System Equivalence Table

The hexadecimal numbering system is used only for the convenience of the programmer or computer scientist, or computer engineer when reading and reviewing the binary display of memory. Computers do not operate or process in hex. During the 1960s and early 1970s, programmers often had to examine the contents of RAM to debug their programs (that is, to eliminate program errors). Today's programming languages have user friendly diagnostics (error messages) and computer-assisted tools that help programmers during program development. These diagnostics and development aids have minimized the need for applications programmers to convert binary and hexadecimal numbers into their more familiar decimal equivalents. However, if you become familiar with these numbering systems, you should achieve a better overall understanding of computers. And, someday you may need to read hex to decode an error message or set the jumpers on an expansion card. PRINCIPLES OF NUMBERING SYSTEMS

Binary The binary, or base-2, numbering system is based on the same principles as the decimal, or base-10, numbering system, with which we are already familiar. The only difference between the two numbering systems is that binary uses only two digits, 0 and 1, and the decimal numbering system uses 10 digits, 0 through 9. The equivalents for binary, decimal, and hexadecimal numbers are shown in Figure 1. The value of a given digit is determined by its relative position in a sequence of digits. Consider the example in Figure 2. If we want to write the number 124 in decimal, the interpretation is almost automatic because of our familiarity with the decimal numbering system. To illustrate the underlying concepts, let's give Ralph, a little green two-fingered Martian, a bag of 124 (decimal) marbles and ask him to express the number of marbles in decimal. Ralph, who is more familiar with binary, would go through the following thought process (see Figure 2).

FIGURE 2 Numbering System Fundamentals Ralph, our two-fingered Martian who is used to counting in binary, might go through the thought process illustrated here when counting 124 marbles in decimal. Ralph's steps are discussed in the text.

Step 1. Ralph knows that the relative position of a digit within a string of digits determines its value, whether the numbering system is binary or decimal. Therefore, the first thing to do is determine the value represented by each digit position.

Step 2. Ralph knows that as in any numbering system, the rightmost position has a value of the base to the zero power, or 1 (100 = 1). The second position is the base to the first power, or 10 (101 = 10). The third position is the base squared, or 100, and so on.

Step 3. Because the largest of the decimal system's 10 digits is 9, the greatest number that can be represented in the rightmost position is 9 (9 X 1). The greatest number that can be represented in the second position, then, is 90 (9 X 10). In the third position, the greatest number is 900, and so on. Having placed the marbles in stacks of 10, Ralph knows immediately that there will be no need for a fourth-position digit (the thousands position). It is apparent, however, that a digit must be placed in the third position. Because placing a 2 in the third position would be too much (200 > 124), Ralph places a 1 in the third position to represent 100 marbles.

Step 4. Ralph must continue to the second position to represent the remaining 24 marbles. In each successive position, Ralph wants to represent as many marbles as possible. In this case, a 2 placed in the second position would represent 20 of the remaining marbles (2 X 101 = 20).

Step 5. There are still four marbles left to be represented. This can be done by inserting a 4 in the rightmost, or "ones," position.

Step 6. The combination of the three numbers in their relative positions represents 124 (decimal). Ralph would go through the same thought process if asked to represent the 124 (decimal) marbles using the binary numbering system (see Figure 3). To make the binary conversion process easier to follow, the computations in Figure 3 are done in the more familiar decimal numbering system. See if you can trace Ralph's steps as you work through Figure 3.

FIGURE 3 Representing a Binary Number To represent 124 marbles in binary, we would follow the same thought process as we would in decimal (see Figure 2), but this time we have only two digits (0 and 1). For ease of understanding, the arithmetic is done in decimal.

Hexadecimal Perhaps the biggest drawback to using the binary numbering system for computer operations is that programmers may have to deal with long and confusing strings of 1s and 0s. To reduce the confusion, the hexadecimal, or base-16, numbering system is used as shorthand to display the binary contents of primary and secondary storage. Notice that the bases of the binary and hexadecimal numbering systems are multiples of 2: 2 and 24, respectively. Because of this, there is a convenient relationship between these numbering systems. The numbering-system equivalence table shown in Figure 1 illustrates that a single hexadecimal digit represents four binary digits (01112 = 716, 11012 = D16, 10102 = A16 where subscripts are used to indicate the base of the numbering system). Notice that in hexadecimal, or "hex," letters are used to represent the six higher order digits. Two hexadecimal digits can be used to represent the eight-bit byte of an EBCDIC equals sign (=) (011111102 is the same as 7E16). Figure 4 illustrates how a string of EBCDIC bits can be reduced to a more recognizable form using hexadecimal.

FIGURE 4 System Expressed in Different Ways The word System is shown as it would appear in input/output, internal binary notation, and hexadecimal notation.

We will now examine how to convert one number in a numbering system to an equivalent number in another numbering system. For example, there are occasions when we might wish to convert a hexadecimal number into its binary equivalent. We shall also learn the fundamentals of numbering-system arithmetic. CONVERTING NUMBERS FROM ONE BASE INTO ANOTHER

Decimal to Binary or Hexadecimal A decimal number can be converted easily into an equivalent number of any base by the use of the division/remainder technique. This two-step technique is illustrated in Figure 5. Follow these steps to convert decimal to binary.

FIGURE 5 Converting a Decimal Number into Its Binary Equivalent Use the twoBstep division/remainder technique to convert a decimal number into an equivalent number of any base.

Step 1. Divide the number (19, in this example) repeatedly by 2, and record the remainder of each division. In the first division, 2 goes into 19 nine times with a remainder of 1. The remainder is always one of the binary digits--0 or 1. In the last division you divide 1 by the base (2) and the remainder is 1. Step 2. Rotate the remainders as shown in Figure 5; the result (10011) is the binary equivalent of a decimal 19.

Figure 6 illustrates how the same division/remainder technique is used to convert a decimal 453 into its hexadecimal equivalent (1C5). In a decimal-to-hex conversion, the remainder is always one of the 16 hex digits.

FIGURE 6 Converting a Decimal Number into Its Hexadecimal Equivalent The two-step division/remainder technique is used to convert a decimal number to its hex equivalent.

Binary to Decimal and Hexadecimal To convert from binary to decimal, multiply the 1s in a binary number by their position values, then sum the products (see Figure 7). In Figure 7, for example, binary 11010 is converted into its decimal equivalent (26). FIGURE 7 Converting a Binary Number into Its Decimal Equivalent Multiply the 1s in a binary number by their position values.

The easiest conversion is binary to hex. To convert binary to hex, simply begin with the 1s position on the right and segment the binary number into groups of four digits each (see Figure 8). Refer to the equivalence table in Figure 2, and assign each group of four binary digits a hex equivalent. Combine your result, and the conversion is complete.

Hexadecimal to Binary To convert hex numbers into binary, perform the grouping procedure for converting binary to hex in reverse (see Figure 8).

FIGURE 8 Converting a Binary Number into Its Hexadecimal Equivalent Place the binary digits in groups of four, then convert the binary number directly to hexadecimal.

Hexadecimal to Decimal Use the same procedure as that used for binary-to-decimal conversions (see Figure 7) to convert hex to decimal. Figure 9 demonstrates the conversion of a hex 3E7 into its decimal equivalent of 999. FIGURE 9 Converting a Hexadecimal Number into Its Decimal Equivalent Multiply the digits in a hexadecimal number by their position values. ARITHMETIC IN BINARY AND HEXADECIMAL The essentials of decimal arithmetic operations have been drilled into us so that we do addition and subtraction almost by instinct. We do binary arithmetic, as well as that of other numbering systems, in the same way that we do decimal arithmetic. The only difference is that we have fewer (binary) or more (hexadecimal) digits to use. Figure 10 illustrates and compares addition and subtraction in decimal with that in binary and hex. Notice in Figure 10 that you carry to and borrow from adjacent positions, just as you do in decimal arithmetic.

FIGURE 10 Binary, Decimal, and Hexadecimal Arithmetic Comparison As you can see, the only difference in doing arithmetic in the various numbering systems is the number of digits used.

Examples of Binary Arithmetic Example 1 Addition Example 2 Addition

Example 3 Subtraction Example 4 Subtraction

Examples of Hexadecimal Arithmetic Example 1 Addition Example 2 Addition

Example 3 Subtraction Example 4 Subtraction

Self-Check

1. The hex numbering system has a base of ______, and the binary numbering system has a base of ______. 2. The value of a particular digit in a number is determined by its relative position in a sequence of digits. (T/F) 3. A single hexadecimal digit can represent how many binary digits: (a) two, (b) three, or (c) four? 4. The bases of the binary and decimal numbering systems are multiples of 2. (T/F) 5. The binary equivalent of a decimal 255 is ______. 6. The binary equivalent of a hexadecimal 1C is ______. 7. The decimal equivalent of a hexadecimal 1B6 is ______. 8. The hexadecimal equivalent of a decimal 129 is ______. 9. The decimal equivalent of a binary 110101 is ______. 10. The hexadecimal equivalent of a binary 1001 is ______. 11. The binary equivalent of a decimal 28 is ______. 12. The binary equivalent of a hexadecimal 35 is ______. 13. The decimal equivalent of a hexadecimal 7 is ______. 14. The hexadecimal equivalent of a decimal 49 is ______. 15. The decimal equivalent of a binary 110110110 is ______. 16. The hexadecimal equivalent of a binary 1110 is ______. 17. The result of 1012 + 112 is ______(in binary). 18. The result of A116 + BC16 + 1016 is ______(in hexadecimal). 19. The result of 6010 + F116 - 10010012 is ______(in decimal). 20. The result of 112 + 278 + 9310 - B16 is ______(in decimal).

Self-test answers. 1. 16, 2. 2. T. 3. 4. 4. F. 5. 11111111. 6. 11100. 7. 438. 8. 81. 9. 53. 10. 9. 11. 11100. 12. 110101. 13. 7. 14. 31. 15. 438. 16. E. 17. 10002. 18. 16D16. 19. 22810. 20. 10810.

Go to top

Note to instructors: Feel free to use or distribute this document to your students for as long as you use a Prentice Hall book by Larry or Nancy Long.

©2000 Prentice-Hall, Inc. A division of Pearson Education Upper Saddle River, New Jersey 07458 Legal Statement