AND C ODES
2-1 N UMBER S YSTEMS
A familiarity with number systems is quite useful when working with programmable controllers, since a basic function of these devices is to represent, store, and operate on numbers, even when performing the simplest of operations. In general, programmable controllers use binary numbers in one form or another to represent various codes and quantities. Although these number operations are transparent for the most part, there are occasions where a knowledge of number systems is helpful.
First, let’s review some basics. The following statements apply to any number system:
• Every number system has a base or radix.
• Every system can be used for counting.
• Every system can be used to represent quantities or codes.
• Every system has a set of symbols.
The base of a number system determines the total number of unique symbols used by that system. The largest-valued symbol always has a value of one less than the base. Since the base defines the number of symbols, it is possible to have a number system of any base. However, number system bases are typically chosen for their convenience. The number systems usually encountered while using programmable controllers are base 2, base 8, base 10, and base 16. These systems are called binary, octal, decimal, and hexadecimal, respectively. To demonstrate the common characteristics of number systems, let’s first turn to the familiar decimal system.
DECIMAL NUMBER SYSTEM
The decimal number system, which is the most common to us, was undoubtedly developed because humans have ten fingers and ten toes. Thus, the base of the decimal number system is 10. The symbols, or digits, used in this system are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. As noted earlier, the total number of symbols (10) is the same as the base, with the largest-valued symbol being
one less than the base (9 is one less than 10). Because the decimal system is so common, we rarely stop to think about how to express a number greater than 9, the largest-valued symbol. It is, however, important to note that the technique for representing a value greater than the largest symbol is the same for any number system.
In the decimal system, a place value, or weight, is assigned to each position that a number greater than 9 would hold, starting from right to left. The first position (see Figure 2-1), starting from the right-most position, is position 0, the second is position 1, and so on, up to the last position n. As shown in Figure 2-2, the weighted value of each position can be expressed as the base (10 in this case) raised to the power of n (the position). For the decimal system, then, the position weights from right to left are 1, 10, 100, 1000, etc. This method for computing the value of a number is known as the sum-of-the-weights method.
Figure 2-1. Place values.
Figure 2-2. Weighted values.
The value of a decimal number is computed by multiplying each digit by the weighted value of its position and then summing the results. Let’s take, for example, the number 9876. It can be expressed through the sum-of-the-weights method as:
Number
Position n. . . 3 2 1 0
Value Vn. . . V3 V2 V1 V0
100= 1 101= 10 102= 100 103= 1000 Weight Value = Base Position
Position (n) 3 2 1 0
Value (V) V3 V2 V1 V0
(Base = 10 for decimal)
6 x 100= 7 x 101= 8 x 102= 9 x 103=
6 70 800 9000 9876
Position 3 2 1 0
Number 9 8 7 6
10
As you will see in other number systems, the decimal equivalent of any number can be computed by multiplying each digit by its base raised to the power of the digit’s position. This is shown below:
Therefore, the sum of N0 through Nn will be the decimal equivalent of the number in base b.
Figure 2-3. The binary numbers, 1 and 0, on a computer’s power switch represent ON and OFF, respectively.
Z0 x b0 = N0
Z1 x b1 = N1 Z2 x b2 = N2
Z3 x b3 = N3
Zn x bn = Nn Position n 3 2 1 0
Number Zn Z3 Z2 Z1 Z0 Base = b
BINARY NUMBER SYSTEM
The binary number system uses the number 2 as the base. Thus, the only allowable digits are 0 and 1; there are no 2s, 3s, etc. For devices such as programmable controllers and digital computers, the binary system is the most useful. It was adopted for convenience, since it is easier to design machines that distinguish between only two entities, or numbers (i.e., 0 and 1), rather than ten, as in decimal. Most physical elements have only two states: a light bulb is on or off, a valve is open or closed, a switch is on or off, and so on. In fact, you see this number system every time you use a computer—if you want to turn it on, you flip the switch to the 1 position; if you want to turn it off, you flip the switch to the 0 position (see Figure 2-3).
Digital circuits can distinguish between two voltage levels (e.g., +5 V and 0 V), which makes the binary system very useful for digital applications.
As with the decimal system, expressing binary numbers greater than the largest-valued symbol (in this case 1) is accomplished by assigning a weighted value to each position from right to left. The weighted value (decimal equivalent) of a binary number is computed the same way as it is for a decimal number—only instead of being 10 raised to the power of the position, it is 2 raised to the power of the position. For binary, then, the weighted values from right to left are 1, 2, 4, 8, 16, 32, 64, etc., representing positions 0, 1, 2, 3, 4, 5, 6, etc. Let’s calculate the decimal value that is equivalent to the value of the binary number 10110110:
Thus, the binary number 10110110 is equivalent to the number 182 in the decimal system. Each digit of a binary number is known as a bit; hence, this particular binary number, 10110110 (182 decimal), has 8 bits. A group of 4 bits is known as a nibble; a group of 8 bits is a byte; and a group of one or more bytes is a word. Figure 2-4 presents a binary number composed of 16 bits, with the least significant bit (LSB), the lowest valued bit in the word, and the most significant bit (MSB), the largest valued bit in the word, identified.
Figure 2-4. One word, two bytes, sixteen bits.
1 0 1 1 1 0 0 1 0 0 1 1 0 1 0 1 Most
Significant Bit
(MSB) Bit
Least Significant Bit
(LSB)
Byte
Word
Byte 0 x 20 = 1 x 21 = 1 x 22 = 0 x 23 = 1 x 24 = 1 x 25 = 0 x 26 = 1 x 27 =
0 2 4 0 16 32 0 128
Position
Number 1 0 1 1 0 1 1 0
3
7 6 5 4 2 1 0
18210 2
Counting in binary is a little more awkward than counting in decimal for the simple reason that we are not used to it. Because the binary number system uses only two digits, we can only count from 0 to 1—only one change in one digit location (OFF to ON) before a new digit position must be added.
Conversely, in the decimal system, we can count from 0 to 9, equaling ten digit transitions, before a new digit position is added.
In binary, just like in decimal, we add another digit position once we run out of transitions. So, when we count in binary, the digit following 0 and 1 is 10 (one-zero, not ten), just like when we count 0, 1, 2…9 in decimal, another digit position is added and the next digit is 10 (ten). Table 2-1 shows a count in binary from 010 to 1510.
Table 2-1. Decimal and binary counting.
OCTAL NUMBER SYSTEM l
a m i c e
D Binary
0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
0
1 1010
1
1 1011
2
1 1100
3
1 1101
4
1 1110
5
1 1111
Writing a number in binary requires substantially more digits than writing it in decimal. For example, 9110 equals 10110112. Too many binary digits can be cumbersome to read and write, especially for humans. Therefore, the octal numbering system is often used to represent binary numbers using fewer digits. The octal number system uses the number 8 as its base, with its eight digits being 0, 1, 2, 3, 4, 5, 6, and 7. Table 2-2 shows both an octal and a binary count representation of the numbers 0 through 15 (decimal).
Table 2-2. Decimal, binary, and octal counting.
As noted earlier, the octal numbering system is used as a convenient way of writing a binary number. The octal system has a base of 8 (23), making it possible to represent any binary number in octal by grouping binary bits in groups of three. In this manner, a very large binary number can be easily represented by an octal number with significantly fewer digits. For example:
Like all other number systems, each digit in an octal number has a weighted decimal value according to its position. For example, the octal number 1767 is equivalent to the decimal number 1015:
l a m i c e
D Binary Octal
0 0 0
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
8 1000 10
9 1001 11
0
1 1010 12
1
1 1011 13
2
1 1100 14
3
1 1101 15
4
1 1110 16
5
1 1111 17
Binary Number
1 1 1 0 0 0 1 1 1 1 1 0 1 0 1 1
3-Bit Groups
1 1 1 0 0 0 1 1 1 1 1 0 1 0 1 1
Octal Digits
1 6 1 7 5 3
7 x 80= 6 x 81= 7 x 82= 1 x 83=
7 48 448 512
Position 3 2 1 0
Number 1 7 6 7 8
101510
So, a 16-bit binary number can be represented directly by six digits in octal.
As you will see later, many programmable controllers use the octal number system for referencing input/output and memory addresses.
The hexadecimal (hex) number system uses 16 as its base. It consists of 16 digits—the numbers 0 through 9 and the letters A through F (which represent the numbers 10 through 15, respectively). The hexadecimal system is used for the same reason as the octal system, to express binary numbers using fewer digits. The hexadecimal numbering system uses one digit to represent four binary digits (or bits), instead of three as in the octal system. Table 2-3 shows a hexadecimal count example of the numbers 0 through 15 with their decimal and binary equivalents.
As with the other number systems, hexadecimal numbers can be represented by their decimal equivalents using the sum-of-the-weights method. The decimal values of the letter-represented hex digits A through F are used when computing the decimal equivalent (10 for A, 11 for B, and so on). The following example uses the sum-of-the-weights method to transform the hexadecimal number F1A6 into its decimal equivalent. The value of A in the example is 10 times 161, while F is 15 times 163. Thus, the hexadecimal number F1A6 is equivalent to the decimal number 61,862:
Table 2-3. Binary, decimal, and hexadecimal counting.
y r a n i
B Decimal Hexadecimal
0 0 0
1 1 1
0
1 2 2
1
1 3 3
0 0
1 4 4
1 0
1 5 5
0 1
1 6 6
1 1
1 7 7
0 0 0
1 8 8
1 0 0
1 9 9
0 1 0
1 10 A
1 1 0
1 11 B
0 0 1
1 12 C
1 0 1
1 13 D
0 1 1
1 14 E
1 1 1
1 15 F
HEXADECIMAL NUMBER SYSTEM
Like octal numbers, hexadecimal numbers can easily be converted to binary without any mathematical transformation. To convert a hexadecimal number to binary, simply write the 4-bit binary equivalent of the hex digit for each position. For example:
1 1 1 1
F 1 A 6
0 0 0 1 1 0 1 0 0 1 1 0 6 x 160= 10 x 161= 1 x 162= 15 x 163=
6 160 256 61440
Position 3 2 1 0
Number F 1 A 6
6186210 16