Digital Logic Design (ES216)
Department of Mechatronic
Engineering
Lec # 9
Engr. Muhammad Arsalan Jalees
Abro
[email protected]
Lecture contents
• BCD Addition
• Gray Code
• Binary to Gray Conversion
• Gray to Binary Conversion
BCD Addition
• There are only ten code groups in the BCD system, so it is very easy to convert between
decimal and BCD.
• The 8421 code is a type of BCD (binary coded decimal) code. Binary coded decimal means
that each decimal digit, 0 through 9, is represented by a binary code of four bits.
• The designation 8421 indicates the binary weights of the four bits .
• The ease of conversion between 8421 code numbers and the familiar decimal numbers is the
main advantage of this code.
• Here is how to add two BCD numbers:
Step 1: Add the two BCD numbers, using the rules for binary addition.
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. Add 6 (0110) to the 4-bit sum in order to skip the six invalid states
and return the code to 8421. If a carry results when 6 is added, simply add the carry
to the next 4-bit group.
Bcd addition
In case of invalid bcd addition
In case of invalid bcd addition
Gray code
• Gray Code is a form of binary that uses a different method of incrementing from one
number to the next.
• Only one bit changes state from one position to another. This feature allows a system
designer to perform some error checking (i.e., if more than one-bit changes, the data must
be incorrect).
• Gray code is also known as reflected binary code (RBC) or reflected binary (RB)
• Frank Gray was the inventor of Gray Code.
• Gray Code is the most popular absolute encoder output type because its use prevents
certain data errors that can occur with Natural Binary during state changes.
• For example, in a highly capacitive circuit (or sluggish system response), a Natural
Binary state change from 0011 to 0100 could cause the counter/PLC to see 0111. This sort
of error is not possible with Gray Code, so the data is more reliable.
GENERATING A GRAY CODE
• Like binary numbers, the Gray code can have any number of bits.
• One method is to convert Binary into Gray Code but that takes time. You should know how to
directly generate a gray code table.
• Following are the steps for the generation of Gray Code:
1. Commence with the simplest Gray code possible; that is, for a single bit.
2. Create a mirror image of the existing Gray code below the original values.
3. Prefix the original values with 0s and the mirrored values with 1s.
4. Repeat steps 2) and 3) until the desired width is achieved.
Addition of 0s to the
Addition of 0s to the Original value
Gray code generation Original value
Mirror Line
Mirror Line
Addition of 1s to the Mirror Line
Mirrored value
Addition of 1s to the
Mirrored value
Four-bit gray code
Binary-to-Gray Code Conversion
• Follow the following rules:
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
get the next Gray code bit. Discard carries.
For example, the conversion of the binary number 10110 to Gray code is as
follows:
MSB → 1 1 1 0 1
The Gray code is 11101
Gray-to-Binary Code Conversion
• Follow the following rules:
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.
For example, the conversion of the Gray code word 11011 to binary is as follows:
MSB → 1 0 0 1 0
The Binary code is 10010
Example
a. Convert the binary number 11000110 to Gray code.
b. Convert the Gray code 10101111 to binary.
Solution:
Applications of Gray Code
1. The Gray code is used in the transmission of digital signals as it minimizes the occurrence of
errors.
2. The Gray code is preferred over the straight binary code in angle-measuring devices. Use of the
Gray code almost eliminates the possibility of an angle misread, which is likely if the angle is
represented in straight binary. The cyclic property of the Gray code (one bit change) is a plus in
this application.
3. The Gray code is used for labeling the axes of Karnaugh maps, a graphical technique used for
the minimization of Boolean expressions.
4. The use of Gray codes to address program memory in computers minimizes power consumption.
This is due to fewer address lines changing state with advances in the program counter.
5. Gray codes are also very useful in genetic algorithms since mutations in the code allow for
mostly incremental changes. However, occasionally a one-bit change can result in a big leap,
thus leading to new properties.