You are on page 1of 18

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

• Explain the hexadecimal number system.


• Convert binary numbers to hexadecimal numbers and vice versa.
• Explain the octal number system.
• Convert binary numbers to octal numbers and vice versa.
• Explain the binary coded decimal number system and its use.
• Convert decimal numbers to binary coded decimal.
• Explain the Gray code number system and its use.
• Explain ASCII and EBCDIC alphanumeric codes.

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.

3.2 Decimal Number System


Decimal number The decimal number system has ten (10) digits running from zero
system: (0) through nine (9). The decimal digits are 0,1, 2, 3, 4, 5, 6, 7, 8, and 9.
Number system that has
a base of ten (10). The value for each digit depends on the position of the digit within the
Base: number. The multipliers for the digits are numbers constructed by the
The number that base, also called the radix, of the number system (10 in the decimal sys­
determines the weight of tem) raised to the appropriate power. Notice in the examples that follow
digits in a given number
system. Also called a
that 10° is equal to 1. Table 3-1 contains values for the power of ten (10)
radix. numbers.

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

Positive powers of 10 Negative powers of 10

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

Table 3-1. Positive and negative powers of 10.

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

3.3 Binary Number System


Binary number system: The binary number system has a base, also called a radix, of two (2).
Number system that has Therefore, only the digits 1 and 0 are present. All binary numbers are
a base of two.
created from combinations of these two digits.
Least significant
bit (LSB): There are three binary numbers in Example 3-4. The first two num­
Bit in a binary number on bers are integer binary numbers and the last one is a real binary num­
the far right-hand side. ber. The binary digit to the far right-hand side of the number is called
Most significant the least sign ificant bit (LSB). Just as with the decimal system, the
bit (MSB):
Bit in a binary number on
rightmost number has the smallest value. The binary digit to the far left-
the far left-hand side. hand side of the number is called the m ost sign ificant bit (MSB).
As you will see in the following example, a subscript 2 following
a number is often used to indicate that it is a binary number.

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)

3.4 Binary-to-Decimal Conversion


Converting between binary and decimal number systems is an
important skill. First, you will learn to convert a binary number to a
decimal.
To convert binary-to-decimal, first convert each bit to its equivalent
decimal value. Then, add all of these decimal values. Example 3-5 illus­
trates how to convert integer and real binary numbers to decimal.

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

3. Convert the real binary number 1001.1011 to a decimal number.


1001.1011 = (1 x 23) + (0 x 22) + (0 x 21) + (1 x 2°) + (1 x 2~l) +
(0 x 2-2) + (1 x 2-3) + (1 x 2-4)
= 8 + 0 + 0 + 1 + 0 5 + 0 + 0.125 + 0.0625
= 9.6875

3.5 Decimal-to-Binary Conversion


Conversion from decimal to binary is also important. The repeated
division or successive division method is used to convert the integer
part of a decimal number. Repeated or successive multiplication is
used to convert the fractional part.
In the su ccessive div ision m ethod, the integer decimal number is Successive division
divided by two, repeatedly. The remainders make up the new binary method:
Method of converting
number. A remainder of one or less indicates a one. No remainder decimal numbers to
indicates a 0. The first division gives you your least significant digit. binary by dividing the
The final division gives you your most significant digit. The division decimal number by two
repeatedly.
continues until the quotient is equal to zero. Example 3-6 illustrates
how to convert integer decimal numbers to binary numbers.

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

process is continued until the decimal part of the number becomes


zero. Example 3-7 illustrates how to convert a fractional decimal
number to a binary number.

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.

3.6 Binary Addition


In the decimal number system, a carry is generated when the sum
of two digits is equal to 10. In the binary number system, a carry is
generated when the sum of two binary bits is equal to two. The fol­
lowing four binary bit additions describe all the ways binary bits are
Carry bit: added and how the carry b it is generated.
Bit in an addition
operation that is 0+0= 0
generated when the sum 0+ 1= 1
of two digits is larger 1 + 1 = 0 with carry of 1
than the base.
1 + 1 + 1 = 1 with carry of 1
Example 3-8 illustrates these binary additions in action.

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

The following illustrates how carry bits are generated from


right to left.
0 + 1 = 1 with carry = 0
0+0+ 0= 0with carry = 0
1+ 0+ 0= 1with carry = 0
1+ 1+ 0= 0with carry = 1
0+ 0+ 1= 1with carry = 0
1+ 1+ 0= 0with carry = 1
1+ 1+ 1= 1with carry = 1
1+ 0+ 1= 0with carry = 1
Nothing + 1 = 1
3. Add the eight-bit binary numbers 11111111 and 11110111.
carry: '11111111
11111111
,11110111
111110110
The following illustrates how carry bits are generated from
right to left.
1 + 1 = 0 with carry = 1
1 + 1 + 1 = 1 with carry = 1
1 + 1 + 1 = 1 with carry = 1
1 + 0 + 1 = 0 with carry = 1
1 + 1 + 1 = 1 with carry = 1
1 + 1 + 1 = 1 with carry = 1
1 + 1 + 1 = 1 with carry = 1
1 + 1 + 1 = 1 with carry = 1
Nothing + 1 = 1

p3.7 Binary Subtraction


In the decimal number system, when the value of a digit is being
subtracted (the su btrahen d) is greater than the value of the digit that it Subtrahend:
is being subtracted from (the m inuend), a borrow from the next higher The bottom number in a
subtraction operation.
column (the column to the left) is required. The same principle applies
Minuend:
to the subtraction of binary numbers. The following four binary bit The top number in a
subtractions describe all the ways binary bits are subtracted and when subtraction operation.
a borrow b it is required. Borrow bit:
Bit in a subtraction
0 - 0 = 0 no borrow operation that is required
1 - 1 = 0 no borrow when the subtrahend
1 - 0 = 1 no borrow (bottom number) digit is
larger than the minuend
(1) 0 - 1 = 1 with a borrow of 1
(top number) digit.
Example 3-9 illustrates how to subtract one binary number from
another binary number.
56 Programmable Logic Controllers: Hardware and Programming

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

■ 3.8 Binary Multiplication


Binary multiplication is similar to decimal multiplication, except
there are only two bits in binary numbers. Notice that in multiplica­
tion, the m u ltiplican d is multiplied by each of the m u ltiplier digits. Multiplicand:
The results of each higher multiplier digit are shifted one place to the The top number in a
multiplication operation.
left. When each of the multiplicand's digits have been multiplied by
Multiplier:
the multiplier digits, all of the numbers are added. The following four The bottom number in a
binary bit multiplications describe the way binary bits are multiplied. multiplication operation.
0x0 = 0
0x1 = 0
1x0 = 0
1x1 = 1
Example 3-10 illustrates how to multiply two binary numbers.

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).

H 3.10 Hexadecimal Number System


In medium to larger programmable logic controllers (PLCs), the
Hexadecimal number h ex ad ecim al num ber system , often just called hex, is used to display
system (hex): the PLC memory map and to carry out the math instructions.
Number system that has
a base of 16.
The hexadecimal number system is created by grouping four
binary bits. Hexadecimal numbers have a base of 16 and therefore
require fifteen digits. The hexadecimal number system uses the
numbers zero (0) through nine (9) for the first ten digits. Then, to
avoid confusion, ten (10) through fifteen (15) are represented by
letters A through F. Digits representing the numbers 10, 11, 12, 13,
14, and 15 are displayed by letters A, B, C, D, E, and F respectively.
Sometimes the capital letter H or the small letter h is placed at the
end of a hexadecimal number to distinguish it from a decimal
number.
Chapter 3 Number Systems and Codes 59

The first sixteen hexadecimal digits, their binary equivalent, and


their decimal equivalent numbers are as follows:
Hexadecimal Binary Decimal
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
A 1010 10
B 1011 11
C 1100 12
D 1101 13
E 1110 14
F 1111 15
Conversion from binary to hex and the reverse is fairly simple.
You can form hex numbers from binary numbers by grouping binary
number bits in groups of four. Swap out each group of four binary bits
with its hex equivalent. The number 111100010111 becomes 1111 0001
0111 which is transformed to F 1 7 or F13h.
Reverse the process to convert to binary. Take each hex digit and
swap it with the binary 4-bit equivalent. The number 37Ah becomes
0011 0111 1010 or 001101111010.

^3.11 Octal Number System


In some smaller programmable logic controllers (called
MicroLogix controllers), the octal number system is used to display
the memory maps and carry out the math instructions. The o c ta l Octal number system:
num ber system has a base of eight. Therefore, only the digits zero (0) Number system that has
a base of 8.
through seven (7) are present. The octal number digits, their binary
equivalent, and their decimal equivalent numbers are as follows:
Octal Binary Decimal
0 000 0
1 001 1
2 010 2
3 011 3
4 100 4
5 101 5
6 110 6
7 111 7
60 Programmable Logic Controllers: Hardware and Programming

As it was with hex numbers, it is easy to convert from binary to


octal and vice versa. You can form an octal number from a binary
number by grouping binary number bits in groups of three. Swap out
each group of three binary bits with its octal equivalent octal digit.
The number 11001111 becomes Oil 001 111 which is transformed to
3 1 7 or 3170.
Reverse the process to convert to binary. Take each octal digit and
swap it with the binary 3-bit equivalent. The number 246 becomes 010
100 110 or 10100110.
The capital letter O is placed at the end of an octal number to dis­
tinguish it from a decimal number.

1 3.12 Binary Coded Decimal System


Inside the memory and microprocessor registers of a PLC, num­
bers are represented in binary. However, programmers and mainte­
nance personnel who work on PLCs are familiar with the decimal
number system. Therefore, a system called binary coded decimal was
invented.
Binary coded decimal In the bin ary cod ed d ecim al (BCD) system , each 4 bits of binary
(BCD) system: numbers are grouped to form a decimal number. Four bit groups rep­
Number system that
uses binary numbers to resenting 10, 11, 12, 13, 14, and 15 are not valid numbers in the BCD
represent decimal digits. number system. BCD is constructed by grouping 4-bit binary num­
bers as we did in binary to hexadecimal conversion. However, in BCD,
only digits zero (0) through nine (9) are valid. The BCD digits and
their equivalent decimal numbers are as follows:
Binary Coded Decimal
Decimal (BCD)
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
Chapter 3 Number Systems and Codes 61

To convert a number from any number system to BCD, first con­


vert the number to decimal. Then, represent each decimal digit with
a 4-bit binary number code. Example 3-12 illustrates how decimal,
binary, and hexadecimal numbers are converted to binary coded deci­
mal numbers. Notice that in the resulting BCD numbers, spaces are
left between each grouping of 4-bit binary numbers.

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

3.13 Gray Code


A closed-loop industrial control system that is used to control the
speed and position of an electric motor shaft is called a servo control.
Optical encoders generate codes that represent the angular positions of
the motor shafts. These codes are usually in a format called Gray code.
The Gray code system is similar to binary. Gray code is converted
to binary prior to arithmetic operation. Note that Gray code is used for
detecting the position of a motor shaft. In the Gray cod e system , only Gray code system:
one digit changes state (turns on or off) as a number increments or Number system similar
to binary, but bits are
decrements. Therefore, the speed of the operation of a control system changed only one at a
using the Gray code increases because only one digit changes with time.
every rotation of the optical encoder disc attached to the motor shaft.
However, there is a trade-off. The disadvantage of using the Gray code
is difficulty in doing arithmetic operations with it.
62 Programmable Logic Controllers: Hardware and Programming

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.

3.14 Alphanumeric Code


Letters, numbers, and special symbols are represented by alpha­
American Standard numeric codes. The American Standard Code for Information
Code for Information Interchange (ASCII) is a 7-bit code. This alphanumeric code is used
Interchange (ASCII):
Code that uses seven to represent alphabets, numbers, characters, and control codes in
bits to represent computer systems. One hundred and twenty eight (27 = 128) different
alphabets, numbers, characters can be represented with the ASCII codes.
characters, and control
words.
As discussed in Chapter 2, data is often sent in bytes (groups of
eight bits). When using ASCII, the eighth bit in the one byte of trans­
mitted data is used for a parity bit.
Another alphanumeric code commonly encountered in com­
Extended Binary Coded puter mainframe equipment is the Extended Binary Coded Decimal
Decimal Interchange Interchange Code (EBCDIC). This code is an 8-bit code. Therefore,
Code (EBCDIC):
Often called Extended two hundred and fifty six (28 = 256) different characters can be rep­
ASCII, a code that uses resented. EBCDIC or commonly called Extended ASCII uses 8-bit
eight bits to represent code, therefore it can represent 256 different alphabets (uppercase
alphabets, numbers,
characters, and control
and lowercase), numbers (1 thru 9), punctuation, control codes, and
words. other characters. Regular ASCII uses only 7 bits; therefore, it can cre­
ate codes for only 128 characters. Table 3-2 contains the ASCII and the
EBCDIC code. Table 3-3 contains a few of the common control codes
used in PC computer systems.
Chapter 3 Number Systems and Codes 63

HEX C ode HEX Code HEX Code HEX Code


ASCII HEX Code EBCDIC HEX Code ASCII EBCDIC ASCII
for for 7-Bit for for 7-Bit EBCDIC for
Symbol for 7-Bit Symbol Symbol Symbol Symbol
ASCII EBCDIC ASCII EBCDIC ASCII Symbol EBCDIC

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

Table 3-2. The ASCII and EBCDIC alphanumeric codes.


Programmable Logic Controllers: Hardware and Programming

NUL Null DC1 Direct control 1


SOH Start o f heading DC2 Direct control 2
STX Start text DC3 Direct control 3
ETX End text DC4 Direct control 4
EOT End of transmission NAK N egative
ENQ Enquiry a c kn o w le d g e
ACK A ckn o w le d g e SYN Synchronous idle
BEL Bell ETB End transmission
BS B ackspace block
HT Horizontal ta b CAN C a n cel
LF Line fe e d EM End o f m edium
VT Vertical ta b SUB Substitute
FF Form fe e d ESC Escape
CR C arriage return FS Form separator
SO Shift out GS G roup separator
SI Shift in RS Record separator
DLE D ata link escape US Unit separator

Table 3-3. Definitions of control codes.

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

• The hexadecimal number system is used to display the PLC


memory map and to carry out the math instructions and is
created by grouping four binary bits. Hexadecimal numbers
have a base of 16, require fifteen digits, and use the numbers
zero (0) through nine (9) for the first ten digits and ten (10)
through fifteen (15) are represented by letters A through F.
• The octal number system is used to display the memory maps
and carry out the math instructions in smaller PLCs. The octal
number system has a base of eight and only the digits zero (0)
through seven (7) are present.
• BCD is constructed by grouping 4-bit binary numbers, however,
in BCD, only digits zero (0) through nine (9) are valid.
• In the Gray code system, only one digit changes state (turns on
or off) as a number increments or decrements.
• Letters, numbers, and special symbols are represented by
alphanumeric codes. The American Standard Code for Information
Interchange (ASCII) is a 7-bit code used to represent alphabets,
numbers, characters, and control codes in computer systems.
• The Extended Binary Coded Decimal Interchange Code
(EBCDIC) is an 8-bit code that represents 256 different alphabets
(uppercase and lowercase), numbers (1 thru 9), punctuation,
control codes, and other characters.

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

4. Add the following binary numbers.


A. 1100 + 1111
B. 11000111 + 11000000
C. 10101011 + 11110011
5. Perform the following binary subtractions.
A. 1100 -1001
B. 10001100-10000101
C. 11100011 -10101010
6. Multiply the following binary numbers.
A. 10101 x 1011
B. 11011100x10101111
7. Solve the following division problems.
A. 11000111 -1010
B. 10101111-1111
8. Convert hexadecimal number AB12h to its decimal equivalent.
9. Convert hexadecimal number 15CD.A2h to its binary equivalent.
10. Convert binary number 110111.00111 to its hexadecimal
equivalent.
11. Convert the following decimal numbers to octal numbers.
A. 25
B. 127
C. 365
12. Convert the following binary numbers to octal numbers.
A. 110111
B. 1111000111.01
C. 1000111001.1
D. 1110011100.0110
13. Convert the following binary numbers to BCD numbers.
A. Ill
B. 1110
C. 10000
D. 100010
E. 100110
14. Write your first name in the ASCII code.
15. Write your last name in the EBCDIC code.

You might also like