EE227 – Digital Logic Design
Lecture 4
Outline
• Binary Codes
– Binary Coded Decimal
– Integer Arithmetic in BCD
• Gray Code
• ASCII Character Code
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 2
Binary Codes (1/2)
• Any discrete element of information that is
distinct among a group of quantities can be
represented with a binary code
– A binary code is just a pattern of 0’s and 1’s
• An n‐bit binary code is a group of n bits that
assumes up to 2n distinct combinations
• Although the minimum number of bits required
to code 2n distinct quantities is n
• There is no maximum number of bits that may be
used for a binary code
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 3
Binary Codes (2/2)
• There are many types of codes used in
digital systems, some of them are
– Binary Coded Decimal (BCD)
– 2421, excess 3, 8,4,-2,-1 codes
– Gray Code
– ASCII Character Code
– Unicode
– And many more
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 4
Binary Coded Decimal (BCD)
• BCD code is used to represent decimal numbers
as a sequence of 0’s and 1’s
• Each decimal digit is written in binary separately
– For example the BCD code of 142 is 0001 0100 0010
• A number with k decimal digits will require 4k
bits in BCD
• It is important to realize that BCD numbers are
decimal numbers and not binary numbers,
although they use bits in their representation
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 5
BCD Addition
• In BCD, each digit cannot exceed 9, so with a possible carry the
maximum sum is 9 + 9 + 1 = 19
• The binary sum will produce a result in the range from 0 to 19
• In binary, this range will be from 0000 to 10011, but in BCD, it is
from 0000 to 1 1001
• When the binary sum is equal to or less than 1001 (without a
carry), the corresponding BCD digit is correct
• When the binary sum is greater than 1001, the result is an
invalid BCD digit
• The addition of 6 = (0110)2 to the binary sum converts it to the
correct digit
• This is because a carry in the most significant bit position of the
binary sum and a decimal carry differ by
16 - 10 = 6
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 6
BCD Addition (Example 1)
• Perform the following BCD Additions
i. 4+5
+ 4 =+ 0100
+5 = +0101 No Correction required
+ 9 =+ 1001
ii. 8+9
+ 18 = +1 1000
+ 19 = +1 1001
BCD Code of 17 is 0001 0111
+ 17 = +1 0001 So a correction is needed
+0 0110 Correction Applied By Adding 6
+1 0111
1 7
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 7
BCD Addition (Example 2)
• The addition of two n‐digit unsigned BCD numbers
follows the same procedure with each digit added and
corrected separately
• For example 184 + 576 will be performed like this
• 184 + 576 = 760
1 (0111 0110
1 0000 in BCD)
184 = 0001 01000 00100
576 = 0101 010010101 0 0 0 1 11 00 1 0
576 = 0111 110 110
10110 10000
576 =
760 = 0 1 71 1 6 0
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 8
Other Decimal Codes
• Other than BCD codes we have
Excess-3 Codes
2 4 2 1 Code
8 4 -2 -1 Code
• BCD Code is called 8 4 2 1 Code as well
• In these code 2 4 2 1, 8 4 -2 -1 and 8 4 2 1
are the weights of individual bits
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 9
Forming the Excess-3 Codes
Excess-3 code can be formed by adding 3 in decimal digit
Decimal Excess-3 Excess-3 BCD Code
Digit Code
0 0+3=3 0011 0000
1 1+3=4 0100 0001
2 2+3=5 0101 0010
3 3+3=6 0110 0011
4 4+3=7 0111 0100
5 5+3=8 1000 0101
6 6+3=9 1001 0110
7 7 + 3 = 10 1010 0111
8 8 + 3 = 11 1011 1000
9 9 + 3 = 12 1100 1001
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 10
Forming the 2421 Codes
We will add the appropriate bit weights to form 2421 codes
Decimal Can be Written as 2421
Digit Code
0 0+0+0+0 0000
1 0+0+0+1 0001
2 0+0+2+0 OR 2+0+0+0 0010 OR 1000
3 0+0+2+1 OR 2+0+0+1 0011 OR 1001
4 0+4+0+0 OR 2+0+2+0 0100 OR 1010
5 0+4+0+1 OR 2+0+2+1 0101 OR 1011
6 0+4+2+0 OR 2+4+0+0 0110 OR 1100
7 0+4+2+1 OR 2+4+0+1 0111 OR 1101
8 2+4+2+0 1110
9 2+4+2+1 1111
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 11
Forming the 8 4 -2 -1 Codes
We will add the appropriate bit weights to form 84-2-1 codes
Decimal Can be Written as 2421
Digit Code
0 0+0–0–0 0000
1 0+4–2–1 0111
2 0+4–2–0 0110
3 0+4–0–1 0101
4 0+4–0–0 0100
5 8+0–2–1 1011
6 8+0–2–0 1010
7 8+0–0–1 1001
8 8+0–0–0 1000
9 8+4–2–1 1111
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 12
Table of Decimal Codes
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 13
Practice Problem 1
1) Represent the unsigned decimal numbers 791
and 658 in BCD, and then show the steps
necessary to form their sum
2) Formulate a weighted binary code for the
decimal digits, using the 6,3,1,1 weights
3) Represent the decimal number 6,248 in
a) BCD
b) excess‐3 code
c) 2421 code
d) 6311 code
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 14
Gray Code
• This coding system has a property that there is only one
bit change between consecutive gray codes
• The Gray code is used in applications in which the
normal sequence of binary numbers generated by the
hardware may produce an error or ambiguity during the
transition from one number to the next
• A typical application of the Gray code is the
representation of analog data by a continuous change in
the angular position of a shaft
• Another application of reduced power consumption due
to reduced switching activity
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 15
Gray Code Table
Decimal
Binary Code Gray Code
Number
0 0000 0000
1 0001 0001
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
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 16
Generating Gray Code Table
• Table of gray codes for any number of bits can be
generated by “Mirror and Prefix” method
• Follow these steps to generate gray code table
i. Start with the simplest Gray code possible; that is, for a
single bit.
ii. Create a mirror image of the existing Gray code below
the original values.
iii. Prefix the original values with 0s and the mirrored
values with 1s.
iv. Repeat steps (2) and (3) until the desired width is
achieved
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 17
Generating Gray Code Table
Mirror
Mirror
Mirror
Start
Prefix
Prefix
Prefix
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 1 0 1 0 0 1 0 0 1 0 0 0 1
1 1 1 1 1 0 1 1 0 1 1 0 0 1 1
0 1 0 1 0 0 1 0 0 1 0 0 0 1 0
1 0 1 1 0 1 1 0 0 1 1 0
1 1 1 1 1 1 1 1 0 1 1 1
0 1 1 0 1 1 0 1 0 1 0 1
0 0 1 0 0 1 0 0 0 1 0 0
1-bit 1 0 0 1 0 0 0
1 0 1 1 0 0 1
2-bit 1 1 1 1 0 1 1
1 1 0 1 0 1 0
0 1 0 1 1 1 0
0 1 1 1 1 1 1
3-bit 0 0 1 1 1 0 1
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal
0 0 0 1 1 0 0
4-bit
Generating Gray Codes from Binary
• Start from Left
– If it is MSB then place it as it is
– Otherwise, Add the bit to the previous bit and
place the sum in GRAY, ignoring any carry
– Repeat step 2 till end
Example
Compute the gray code of (10010)2
+ + + +
Solution Binary Bits
0+1=1 0+0=0 1+0=1 1+1=10
1 0 0 1 1Ignore the carry
Gray Bits 1 1 0 1 0
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 19
ASCII Character Code (1/2)
• American Standard Code for Information Interchange
(ASCII) is a 7-bit code which can be used for
alphanumeric data
• The 7 bits are designated through b1 to b7 with b7 the
MSB
• There are 94 graphic characters (which can be printed)
– Upper case and lower case letters, 0 – 9 digits and some
special characters
• And 34 non-printing characters which are used for
control purposes
– ESC, SP, ACK, NAK etc
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 20
ASCII Character Code (2/2)
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 21
How to Read the ASCII Table?
• Let’s find the ASCII code for “B”, g, and 4.
The 7-bit ASCII code of B is (100 0010)2 = (166)10 = (42)16
The 7-bit ASCII code of g is (110 0111)2 = (103)10 = (67)16
The 7-bit ASCII code of 4 is (011 0100)2 = (152)10 = (34)16
EE227 - DLD Course Instructor : Muhammad Sajid Iqbal 22