Wolkite University
College of Engineering and Technology
Department of Electrical and Computer
Engineering
Digital Logic Design
(ECEg-3141)
1 Lecture: Digital Codes
Habtamu E.
Outlines
2
BCD
BCD Addition
Excess 3 code
Gray Code
Binary-to-Gray Code Conversion
Gray-to-Binary code Conversion
ASCII
BCD
3
BCD number stands for binary coded decimal.
These numbers are not greater than 9.
if number is greater than 9, 6 is added to get the
required value.
Each number is represented by 4 bit binary number.
The decimal values should be represented in binary
number but not in any other form of number systems.
Cont’d
4
BCD is a way to express each of the decimal digits
with a binary code.
There are only 10 code groups in the BCD system.
0 1 2 3 4 5 6 7 8 9
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Ex : 3510 = 0011 0101
Cont’d
5
In BCD, 4-bits represent each decimal digit.
To express any decimal number in BCD, simply
replace each decimal digit with the
appropriate 4 – bit code.
Example: convert each of the following
decimal numbers to BCD.
a. 34 b. 98 c. 160 d. 2467
Cont’d
6
It is equally easy to understand a decimal number
from a BCD number.
Start at the right most bit and break the code into
groups of four bits.
Then write the decimal digit represented by each 4 –
bit group.
Example: convert each of the following BCD codes to
decimal.
a. 00101001 b. 010000101000
BCD addition
7
BCD is a numerical code and can be used in arithmetic
operations.
Addition is the most important operation because the
other three operations (subtraction, multiplication,
and division) can be accomplished by the use of
addition.
Here is how to add two binary numbers:
Step 1: add the two BCD numbers , using the rules
for binary addition.
Cont’d
8
Step 2: if a 4-bit sum is equal to or less than 9, it is
a valid BCD number.
Step 3: if a 4-bit sum is greater than 9, or if a carry
out of the 4-bit group is generated, it is an invalid
result.
o Add 6 (0110) to the 4-bit sum in order to skip the
six invalid states and return the code to 8421.
o if a carry results when 6 is add, simply add the carry
to the next 4-bit group.
Example
9
1. Add the following BCD numbers
a. 0001 + 0100 b. 00100011 + 00010100
c. 10000110 + 00010010
Note that in each case the sum in any 4-bit column
does not exceed 9, and the results are valid BCD
numbers.
Add the following number using BCD.
a. 9 + 4 b. 9+ 9 c. 16 + 15 d. 67 + 53
Exercise
10
1. Add the following numbers using BCD
a. 29 + 17
b. 421 + 127
c. 268 + 474
Excess 3 code
11
The excess 3 code is obtained from BCD code by
adding 3(0011) with the number.
An excess 3 code is a self complement code.
Most of the application of the excess 3 code will be
in subtraction operation in digital computers.
Example: the excess 3 code of 0011 is 6.
0011
+ 0011
0100
Excess 3 code table
12
. Decimal BCD code Excess 3
0 0000 0011
1 0001 0100
2 0010 0101
3 0011 0110
4 0100 0111
5 0101 1000
6 0110 1001
7 0111 1010
8 1000 1011
9 1001 1100
Digital Codes
13
There are other specialized codes used in digital
systems e.g. Gray code, ASCII code.
Some codes are
strictly numeric : e.g. BCD
Alphanumeric – to represent numbers, letters,
symbols, instructions : e.g. ASCII
Gray Code
14
Gray code is not an arithmetic code.
Only one bit changes from one code to the next in
the sequence.
Gray code can be any amounts of bits.
Binary-to Gray code conversion
15
Conversion between binary and gray code is sometimes
useful.
The following rules explain how to convert from a binary
number to a gray cod word:
1. The most significant bit ( left most) in the gray code is
the same as the corresponding MSB in the binary
number.
2. Going from left to right, add each adjacent pair of
binary code bits to bet the next gray code bit. Discard
carries.
Example
16
convert the binary number 1110 to gray code.
1 + 1 + 1 + 0
1 0 0 1
the gray code is 1001.
Gray-to-Binary Conversion
17
To convert from gray code to binary, use a similar
method; however, there are some differences. The
following rules apply:
1. The most significant bit ( left most ) in the binary
code is the same as the corresponding bit in the
gray code.
2. Add each binary code bit generated to the gray
code bit in the next adjacent position. Discard
carries.
Example
18
Convert the gray code word 1110 to binary.
1 1 1 0
+ + +
1 0 1 1
The binary number is 1011.
Exercise
19
1. Find the Excess 3 code of the number 596.
2. Convert following binary number into gray
code and vise-versa: 10101111
DECIMAL BINARY GRAY CODE
0 0000 0000
1 0001 0001
20
2 0010 0011
3 0011 0010
4 0100 0110
5 0101 0111
6 0110 0101
7 0111 0100
8 1000 1100
9 1001 1101
10 1010 1111
11 1011 1110
12 1100 1010
13 1101 1011
14 1110 1001
15 1111 1000
ASCII
21
ASCII is the abbreviation for American Standard Code
for Information Interchange. Pronounced “askee”.
ASCII is a universally accepted alphanumeric code used
in most computers and other electronic equipment.
Most computer keyboards are standardized with the
ASCII.
When you enter a letter, a number, or control
command, the corresponding ASCII code goes into the
computer.
ASCII Cont’d
22
ASCII has 128 characters and symbols represented
by a 7-bit binary code.
Actually, ASCII can be considered an 8-bit code with
the MSB always 0.
The 8-bit code is 00 through 7F in hexadecimal.
The first thirty-two ASCII characters are
nongraphic commands that are never printed or
displayed and are used only for control purposes.
ASCII Cont’d
23
American Standard Code for Information
Interchange (ASCII)
It has 128 characters and symbols represented in 7-
bit binary code.
Example :
A = 10000012;
a = 11000012
ASCII Cont’d
24
Examples of the control characters are “null”,
“line feed”, “start of text”, and “escape”.
The other characters are graphic symbols
that can be printed or displayed and include
the letters of the alphabet (lower case and
Uppercase), the ten decimal digits,
punctuation signs and other commonly used
symbols.
ASCII Cont’d
25
Table shown on the next slide is a listing of the
ASCII code showing the decimal, hexadecimal, and
binary representations for each character and
symbol.
The left section of the table lists the names of the
of the 32 control characters (00 through 1F
hexadecimal).
the graphic symbols are listed in the rest of the
table (20 through 7F hexadecimal).
ASCII Table ASCII : 10000012
26 000 001 010 011 100 101 110 111
0000 NUL DLE SP 0 @ P ` p
0001 SOH DCI ! 1 A Q a q
0010 STX DC2 " 2 B R b r
0011 ETX DC3 # 3 C S c s
0100 EOT DC4 $ 4 D T d t
0101 ENQ NAK % 5 E U e u
0110 ACK SYN & 6 F V f v
0111 BEL ETB ' 7 G W g w
1000 BS CAN ( 8 H X h x
1001 HT EM ) 9 I Y i y
1010 LF SUB * : J Z j z
1011 VT ESC + ; K [ k {
1100 FF FS , < L \ l |
1101 CR GS - = M ] m }
1110 SO RS . > N ^ n ~
1111 SI US / ? O _ o DEL
ASCII Cont’d
27
1. Example: determine the binary ASCII codes that
are entered from the computer’s keyboard when the
following BASIC program statement is typed in. Also
express each code in hexadecimal.
20 PRINT A=“X”;
Symbol Binary Hexadecimal
2 0110010 32
28 0 0110000 30
ASCII solution:
Space 0100000 20
P 1010000 50
R 1010010 52
20 PRINT A=“X”; I 1001001 49
N 1001110 4E
T 1010100 54
Space 0100000 20
A 1000001 41
= 0111101 3D
10000012 = 0100 00012 “ 0100010 22
4 1 X 1011000 3B
“ 0100010 22
; 0111011 3B
29
THANK YOU!!