Programmable Logic Controls, Chapter 3 Number Systems and Codes
Programmable Logic Controls, Chapter 3 Number Systems and Codes
Chapter Outline
3.1 Introduction
3.2 Decimal Number System
3.3 Binary Number System
3.4 Binary-to-Decimal Conversion
3.5 Decimal-to-Binary Conversion
3.6 Binary Addition
3.7 Binary Subtraction
3.8 Binary Multiplication
3.9 Binary Division
3.10 Hexadecimal Number System
3.11 Octal Number System
3.12 Binary Coded Decimal System
3.13 Gray Code
3.14 Alphanumeric Code
Technical Terms
decimal number system subtrahend octal number system
base minuend binary coded decimal
fractional numbers borrow bit (BCD) system
real numbers multiplicand Gray code system
integer numbers multiplier American Standard Code
binary number system divisor for Information
least significant bit (LSB) dividend Interchange (ASCII)
most significant bit (MSB) quotient Extended Binary-Coded
successive division method hexadecimal number Decimal Interchange
carry bit system (hex) Code (EBCDIC)
Learning Objectives
After completing this chapter, you will be able to:
• Explain the decimal number system.
• Explain integer numbers, fractional numbers, and real numbers.
• Describe the binary number system.
• Convert decimal numbers to binary and vice versa.
• Perform binary math operations such as add, subtract, multiply,
and divide. 49
50 Programmable Logic Controllers: Hardware and Programming
3.1 Introduction
The decimal number system, which has a base of ten (10), has
been used as a number system since people started using numbers.
This number system was invented because humans used their ten fin
gers to keep track of their counting.
However, digital devices such as programmable logic controllers
use a two-state logic system in their central processing unit to count
numbers and perform calculations. In two-state logic, the state is either
high (e.g., +5 VDC) or low (e.g., 0 VDC). The high state is assigned to
represent the number one (1) and the low state to represent the num
ber zero (0). This type of number system is called the binary number
system.
In this chapter, you will learn the binary number system and several
other number systems that are used in digital devices. You will learn how
to convert a number from one system to another. You will also learn how
to perform math operations using the binary number system.
Example 3-1
6310 - (6 x 103) + (3 x 102) + (1 x 101) + (0 x 10°)
= (6 x 1000) + (3 x 100) + (1 x 10) + (0 x l )
= 6000 + 300 + 10 + 0
= 6310
Chapter 3 Number Systems and Codes 51
o
o
o
II
II
o o
o o
io-2 = 0.01
II II
CN
o
10-3 = 0.001
103 = 1000 10“4 = 0.0001
104 = 10,000 10-5 = 0.00001
105 = 100,000 10"6 = 0.000001
Fractional numbers:
Numbers that have
F raction al num bers have weights that are negative powers of weights with negative
ten (10). The number in Example 3-2 is a fractional number. powers.
Example 3-2
0.812 = (8 x 10-1) + (1 x 10-2) + (2 x 10~3)
= (8 x 0.1) + (1 x 0.01) + (2 x 0.001)
= 0.8 + 0.01 + 0.002
= 0.812
R eal num bers have weights that are both positive and nega Real numbers:
tive powers of ten (10). The number in Example 3-3 is a real number. Numbers that have both
integer and fractional
Real numbers have both integer and fractional parts. In Example 3-3, parts.
you can see how the value of digits depends on the digits and their
positions.
Example 3-3
547.165 - (5 x 102) + (4 x 101) + (7 x 10°) + (1 x 10"1) + (6 x 10“2) + (5 x 10“3)
= (5 x 100) + (4 x 10) + (7 x 1) + (1 x 0.1) + (6 x 0.01) + (5 x 0.001)
- 500 + 40 + 7 + 0.1 + 0.06 + 0.005
= 547.165
In Example 3-3, there are digits to the right and to the left of the
decimal point. The digits to the left of the decimal point represent the
integer number. The digits to the right of the decimal point represent
the fractional number.
Integer num bers have weights that are positive powers of ten (10). Integer numbers:
These numbers can be positive or negative. Zero is also an integer Numbers that have
weights with positive
number. The number in Example 3-1 is a positive integer number. powers.
52 Programmable Logic Controllers: Hardware and Programming
Example 3-4
(11001)2 = (1x 24) + (1 x 23) + (0 x 22) + (0 x 21) + (1 x 2°)
=16 + 8 + 0 + 0 + 1 = 25
(11101)2 = (1x 24) + (1 x 23) + (1 x 22) + (0 x 21) + (1 x 2°)
(111.0111)2 = (1x 22) + (1 x 21) + (1 x 2°) + (0x 2-1) + (1 x 2~2) + (1 x 2~3)
+ (1 x 2~4)
Example 3-5
1. Convert the integer binary number 11101 to a decimal number.
(11101)2 = (1 x 24) + (1 x 23) + (1 x 22) + (0 x 21) + (1 x 2°)
= 16 + 8 + 4 + 0 + 1 = (29)10 = 29
Note that the base ten (10) is usually not displayed with the
number. The base of 10 is often assumed. Now, work through
some real binary numbers.
2. Convert the real binary number 111.0111 to a decimal number.
(111.0111)2 = (1 x 22) + (1 x 21) + (1 x 2°) + (0 x 2_1) + (1 x 2~2) +
(1 x 2'3) + (1 x 2"4)
= 4 + 2 + l + 0 + 0.25 + 0.125 + 0.0625
- (7.4375)10
= 7.4375
Chapter 3 Number Systems and Codes 53
Example 3-6
1. Convert the integer decimal number 35 to binary.
Quotient Remainder Binary Digit
35 - 2 = 17 1 LSB = remainder = 1
17 - 2 = 8 1 next bit = 1
8-2=4 0 next bit = 0
4-2=2 0 next bit = 0
2 - 2=1 0 next bit = 0
1 - 2=0 1 MSB = remainder = 1
The answer is 100011.
2. Convert the integer decimal number 50 to binary.
Quotient Remainder Binary Digit
50 - 2 = 25 0 LSB = remainder = 0
25 - 2 = 12 1 next bit = 1
12 - 2 = 6 0 next bit = 0
6-2=3 0 next bit = 0
3-2=1 1 next bit = 1
1 - 2=0 1 MSB = remainder = 1
The answer is 110010.
Converting the fractional part of a decimal number to binary
is done using the successive multiplication method. The decimal
number is multiplied by two and the portion that is carried (if the
product is greater than 0) is the MSB of the binary number. This
54 Programmable Logic Controllers: Hardware and Programming
Example 3-7
Convert the fractional decimal number 0.625 to binary.
Carry Fractional Part Binary Digit
0.625 x 2 = 1 0.25 MSB - carry = 1
0.25 x 2 = 0 0.50 next bit = 0
0.50 x 2 = 1 0.0 LSB = 1
The answer is 0.101.
Example 3-8
1. Add the four-bit binary numbers 1111 and 1010.
carry: ' 110
1111
+> ,1010
11001
The following illustrates how carry bits are generated from
right to left.
1+ 0= 1 with carry = 0
1+ 1+ 0 = 0with carry = 1
1+ 0+ 1 = 0with carry = 1
1+ 1+ 1 = 1with carry = 1
Nothing + 1 = 1
2. Add the eight-bit binary numbers 11101100 and 01101001.
carry: 1101000
11101100
,01101001
101010101
Chapter 3 Number Systems and Codes 55
Example 3-9
1. Subtract the four-bit binary number 1010 from the binary
number 1101.
borrow: 0010
1101 minuend
- 1010 subtrahend
0011 result
The following illustrates how borrow bits are generated from
right to left.
1 - 0 = 1 with borrow = 0
(1) 0 - 1 = 1 with borrow = 1 (weight of 2)
0 - 0 = 0 with borrow = 0
1 - 1 = 0 with borrow = 0
2. Subtract the binary number 1111 from the binary number 11110011.
borrow: 00001000
10010111
- 10010011
00000100
The following illustrates how borrow bits are generated from
right to left.
1 - 1 = 0 with borrow = 0
1 - 1 = 0 with borrow = 0
(1) 0 - 1 = 1 with borrow = 1 (weight of 2)
1 - 1 = 0 with borrow = 1 (weight of 1 because previous bit
borrowed from this borrow)
0 - 0 = 0 with borrow = 0
1 - 0 = 1 with borrow = 0
1 - 0 = 1 with borrow = 0
1 - 0 = 1 with borrow = 0
3. Subtract the eight-bit binary number 10010011 from the binary
number 10010111.
borrow: 00001000
10010111
- 10010011
00000100
1 - 1 = 0 with borrow = 0
1 - 1 = 0 with borrow = 0
1 - 0 = 1 with borrow = 0
0 - 0 = 0 with borrow = 0
1 - 1 = 0 with borrow = 0
0 - 0 = 0 with borrow = 0
0 - 0 = 0 with borrow = 0
1 - 1 = 0 with borrow = 0
Chapter 3 Number Systems and Codes 57
Example 3-10
1. Multiply the binary numbers 1011 to 1100.
1011 multiplicand
x 1100 multiplier
0000 Result of the multiplicand times the LSB of the multiplier
(0 in this example)
+ 0000
+ 1011 Result of the multiplicand times the MSB of the multiplier
+ 1011 (1 in this example)
10000100 product
2. Multiply the binary numbers 1111 by 11.
1111
x 0011
1111 Result of the multiplicand times the LSB of the multiplier
(1 in this example)
+ 1111
+ 0000
+ 0000
0101101 Result of the multiplicand times the MSB of the multiplier
(0 in this example)
Divisor:
Number in a division
§|3.9 Binary Division operation that can be
subtracted from the
Division can be explained as how many times a number called the dividend repeatedly.
divisor can be subtracted from another number called the dividend. Dividend:
The subtraction routine continues until the remainder is less than the Number in a division
operation from which the
divisor can be subtracted
repeatedly.
58 Programmable Logic Controllers: Hardware and Programming
Quotient: dividend. The number of times the divisor can be subtracted from
In a division operation, the dividend is called the qu otien t. Binary division is very similar to
it is the number of
times the divisor can decimal division.
be subtracted from the Example 3-11 illustrates how to divide a binary number by another
dividend. binary number using the repeated subtraction method.
Example 3-11
Divide the binary number 11000 by 110 using the repeated sub
traction method.
11000 dividend
- 110 divisor
10010
Next, 10010
-1 1 0
01100
Next, 01100
-1 1 0
00110
Next, 00110
-1 1 0
000 remainder is less than divisor, therefore stop.
Four subtractions took place, therefore the quotient is equal
to 100 binary number which is equal to decimal 4 (100 binary =
4 decimal).
Example 3-12
1. Convert the decimal number 273.125 to BCD.
Simply convert each digit into its decimal equivalent.
2 7 3 1 2 5
0010 0111 0011 0001 0010 0101
Therefore, 273.125 = 0010 0111 0011. 0001 0010 0101BCD
2. Convert binary number 1101.10 to BCD.
First, convert to decimal.
1101.10 = (1 x 23) + (1 x 22) + (0 x 21) + (1 x 2°) + (1 x 2~x) + (0 x 2~2)
= 8 + 4 + 0 + l + 0.5 + 0 = 13.5 decimal
Then, convert to BCD.
13.5 = 0001 0011. 0101
Therefore, 1101.102 = 0001 0011. 0101BCD
3. Convert hex number 1AB to BCD.
To convert hex to BCD, first convert the hex number into
decimal.
1ABh = (1 x 162) + (10 x 161) + (11 x 16°) = 427 decimal
Next, convert each decimal digit into its binary equivalent.
4 2 7
0100 00 1 0 0111 = 0100 0010 O H I bcd
The Gray code, the binary equivalent, and their decimal equiva
lent numbers are as follows.
Gray code Binary Decimal
0000 0000 0
0001 0001 1
0011 0010 2
0010 0011 3
0110 0100 4
0111 0101 5
0101 0110 6
0100 0111 7
1100 1000 8
1101 1001 9
1111 1010 10
1110 1011 11
1010 1100 12
1011 1101 13
1001 1110 14
1000 1111 15
Compare the Gray code to its binary equivalent and observe the changes.
Notice how the combinations of Os and 1s in the Gray code never change by
more than one bit between any two consecutive positions. When a number
increments from 12 to 13, only one bit in the Gray code changes state.
NUL 00 NUL 00 * 2A * 5C T 54 T E3
SOH 01 SOH 01 + 2B + 4E U 55 U E4
STX 02 STX 02 z 2C , 6B V 56 V E5
ETX 03 ETX 03 - 2D - 60 W 57 W E6
EOT 04 EOT 37 2E 4B X 58 X E7
ENQ 05 ENQ 2D / 2F / 61 Y 59 Y E8
ACK 06 ACK 2E 0 30 0 F0 Z 5A Z E9
BEL 07 BEL 2F 1 31 1 FI ( 5B ( AD
BS 08 BS 16 2 32 2 F2 \ 5C NL 15
HT 09 HT 05 3 33 3 F3 ) 5D ) DD
LF 0A LF 25 4 34 4 F4 A 5E 5F
VT 0B VT 0B 5 35 5 F5 5F 6D
FF OC FF 0C 6 36 6 F6 ' 60 RES 14
CR 0D CR 0D 7 37 7 F7 a 61 a 81
SO OE SO 0E 8 38 8 F8 b 62 b 82
SI OF SI OF 9 39 9 F9 c 63 c 83
DLE 10 DLE 10 3A 7A d 64 d 84
DC1 11 DC1 11 ; 3B ; 5E e 65 e 85
DC2 12 DC2 12 < 3C - 4C f 66 f 86
DC3 13 DC3 13 = 3D = 7E g 67 g 87
DC4 14 DC4 35 > 3E \ 6E h 68 h 88
NAK 15 NAK 3D ? 3F ? 6F i 69 1 89
SYN 16 SYN 32 @ 40 @ 7C j 6A j 91
ETB 17 EOB 26 A 41 A Cl k 6B k 92
CAN 18 CAN 18 B 42 B C2 1 6C 1 93
EM 19 EM 19 C 43 C C3 m 6D m 94
SUB 1A SUB 3F D 44 D C4 n 6E n 95
ESC IB BYP 24 E 45 E C5 o 6F o 96
FS 1C FLS 1C F 46 F C6 P 70 P 97
GS ID GS ID G 47 G C7 q 71 q 98
RS IE RDS IE H 48 H C8 r 72 r 99
US IF US IF 1 49 1 C9 s 73 s A2
SP 20 SP 40 J 4A J D1 t 74 t A3
! 21 ! 5A K 4B K D2 u 75 u A4
" 22 " 7F L 4C L D3 V 76 V A5
# 23 # 7B M 4D M D4 w 77 w A6
S 24 $ 5B N 4E N D5 X 78 X A7
% 25 % 6C O 4F O D6 y 79 y A8
& 26 8c 50 P 50 P D7 z 7A z A9
' 27 ' 7D Q 51 Q D8 { 7B { 8B
( 28 ( 4D R 52 R D9 1 7C 1 4F
) 29 ) 5D S 53 S E2 } 7D } 9B
7E 0 4A
DEL 7F DEL 07
1 Summary
^ • The number systems commonly used with PLCs are the decimal
number system, binary number system, hexadecimal number
system, octal number system, binary coded decimal system,
Gray code system, and alphanumeric codes.
• The decimal number system has ten (10) digits running from
zero (0) through nine (9).
• The value for each digit in a decimal number system depends on
the position of the digit within the number.
• The binary number system consists of only the numbers zero (0)
and one (1).
• To convert binary to decimal, you must first convert each bit to its
equivalent decimal value and then add all of these decimal values.
• To convert decimal to binary, the repeated division or successive
division method is used to convert the integer part of a decimal
number and the repeated or successive multiplication is used to
convert the fractional part.
• In the binary number system, a carry is generated when the sum
of two binary bits is equal to two.
• In binary multiplication, when each of the multiplicand's digits
have been multiplied by the multiplier digits, all of the numbers
are added.
• Binary division is very similar to decimal division where the
divisor can be subtracted from another number called the dividend
and the subtraction routine continues until the remainder is less
than the dividend.
Chapter 3 Number Systems and Codes 65
Review Questions
1. Find the maximum decimal values for the following groups of
binary bits (the value if all bits in the group are 1).
A. 4-bit (called a nibble)
B. 8-bit (called a byte)
C. 16-bit (called a word)
D. 32-bit (called a double-word)
E. 64-bit (called a quad word)
2. Convert the following binary numbers to decimal numbers.
A. 1101.11
B. 10011101.01
C. 101101100101.101
D. 1010110011101110.001
3. Convert the following decimal numbers to binary numbers.
A. 2.5
B. 3.625
C. 65
D. 25.125
E. 217.00125
Programmable Logic Controllers: Hardware and Programming