L2 Digital Coding
L2 Digital Coding
Digital Coding
We have learned that decimal, octal and hex numbers can be represented
by binary digits.
Not only numbers, but letters and other symbols, can be represented by
1s and 0s. In fact, any entity expressible as numbers, letters, or other
symbols can be represented by binary digit, and therefore, can be
processed by digital logic circuits. A complete group of these
combinations which represent number, letters or symbols is called digital
code.
The group of 0 s and 1s in the binary number can be thought of as a code
representing the decimal numbers. When a decimal number is represented
by its equivalent binary number, it is called a straight binary coding.
All digital system uses some form of binary numbers for their internal
operation, but the external word is decimal in nature. Conversions
between the decimal and binary system are, therefore, often performed.
But conversions between the decimal and binary become long and
complicated when large numbers are involved. So, we need a mean of
encoding decimal numbers that combines some features of both the
decimal and binary number systems.
The BCD code means that each decimal digit is represented by binary
code of four bits. Clearly only 4–bits binary numbers from 0000 through
1001 are used. The BCD code does not use the numbers 1010, 1011,
1100, 1101, 1110, and 1111. In other words, only 10 of the sixteen (2 4 )
possible 4-bits binary code groups are used. If any of the ‘forbidden’ 4-bit
number ever occurs in a machine using the BCD code, it is usually an
indication that an error has occurred.
Any decimal number can be expressed in BCD code by replacing
each decimal digit by the appropriate 4-bit combinations. Ten binary
combinations that represent the ten decimal digits are shown in Table 1.
Conversely, a BCD number can be easily converted into a decimal
number by dividing the coded number into groups of 4-bit (starting with
LBS) and then writing down the decimal digit represented by each four-
bit group.
It is very important to realize that BCD is not another number system like
binary, octal, decimal, and hexadecimal. It is, in fact, the decimal system
with each decimal digit encoded in its binary equivalent. It is also
important to note that BCD number is not the same number as straight
binary number. A straight binary code takes the complete decimal
number and represents it in binary while the BCD code converts each
decimal digit to binary individually.
BCD Addition
BCD code is numerical code, and many applications need that arithmetic
so that the operation is carried out. Addition is the most important
operation because the other three operation (subtraction, multiplication
and division) can be accomplished.
The rules of addition are:
1. Add the two numbers, using the rules for binary addition.
2. If a four-bit sum is equal to 9 or less than 0, it is a valid BCD
number.
3. if a four-bit sum exceeds 9, or if a carry-out of the group is
generated, it is an invalid result then, add 6 (0110)2 to the four –bit
sum.
4. If a carry results when (0110)2 is added, simply add the carry to
the next four-bit group.
b- 1 4 9 7
0001 0100 1001 0111
So (1497)10 = 0001 0100 1001 0111 BCD Ans.
c- 1001
+ 0101
______
1110 invalid as BCD number exceeds 9
+ 0110 So, add 6
______
0001 0100 Ans.
d- 1001
+ 1010
_____
10011 invalid because of carry
+ 0110 So, add 6
______
0001 1001 Ans.
e- 0011 0110
+ 0001 0101
_______________ right group is invalid as BCD number exceeds 9
0100 1011 left group is valid. Add 6 to invalid group
+ 0110 ( add carry , 0001 , to next group )
________________
0101 0001 valid BCD number Ans.
f- 0111 0111
+ 0101 0011
___________
1100 1010 Both group are invalid > 9
+0110 +0110 Add 6 to both group
Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)
5
University of Bahrain Lesson 2 EENG251
____________
0001 0011 0000 Ans. valid BCD number.
EXCESS-3 CODE:
The excess-3 code, abbreviated as XS-3, is an important 4-bit code
sometimes used with binary-coded decimal (BCD) numbers. It processes
advantages in certain arithmetic operations. The excess-3 code for a
decimal number can be obtained in the same manner as BCD except that
3 is added to each decimal digit before encoding it in binary. For
example, to encode the decimal digit 5 into excess-3 code, we must first
add 3 to obtain 8. The digit 8 is encoded in its equivalent 4-bit binary
code 1000.
Table 2.
2 6
+ 3 +3
add 3 to each digit
5 9
0101 1001 convert to 4-bit binary code
GRAY CODE:
This code belong to a class of cods called minimum-change code ,
in which only one bit in the code group changes when going from
one step to the next . This is an unweighted code, which means that
there are no specific weighs assigned to the bit position. Because of
this, the Gray code is not suited for arithmetic operations but finds
applications in input/ output devices and some types of analog-to-
digital converters (ADCs). Using the Gray code will speed up the
system execution compering with the normal codes, for this reason
PLCs are using Gray code.
The Gray code representation for the decimal numbers 0 through
15, together with the straight binary code are shown in Table 3.
By examining above table for gray code groups for each decimal
numbers it can be seen that going from any one decimal number to the
next , only one bit of Gray code change . For instance, when going
from decimal 3 to 4, the Gray code change from 0010 to 0110, which
only the second bit from the left changing. Going from 14 th 15 the
Gray-code bits change from 1001 to 1000 with only the last bit
changing. This is the principle characteristic of the Gray code.
Because of this feature, an amount of switching is minimized and the
reliability of the switching system is improved. This code is used
extensively for shaft encoders, where errors susceptibility increases
when the number of bit changes between adjacent numbers in a
sequence
Table 3.
Decimal Binary Gray Decimal Binary Gray
Code Code Code Code
0 0000 0000 8 1000 1100
1 0001 0001 9 1001 1101
2 0010 0011 10 1010 1111
3 0011 0010 11 1011 1110
4 0100 0110 12 1100 1010
5 0101 0111 13 1101 1011
6 0110 0110 14 1110 1001
7 0111 0100 15 1111 1000
Binary-to-Gray Conversion.
For conversion of binary number into a gray code number, Following
rules have to be applied:
1. The most significant digit (left - digit) in the gray code is
the same as the corresponding digit in the binary number.
2. Going from left to right, add each adjacent pair of binary
digits to get the next gray code digit. Discard carries if
any.
Example 1: Convert the binary number (10110)2 into a gray code
number.
Solution:
Step 1: the left –most gray digit is the same as the left most binary digit
1 0 1 1 0 Binary code
1 Gray code
Step 2 : add the left most binary digit to the adjacent one
1 + 0 1 1 0 Binary code
1 1 Gray code
Step 3: Add the next adjacent pair
1 0 + 1 1 0 Binary code
1 1 1 Gray code
Step 4: Add the next adjacent pair. Discard carry
1 0 1 + 1 0 Binary code
1 1 1 0 Gray code
Step 5: Add the last adjacent pair.
1 0 1 1 + 0 Binary code
1 1 1 0 1 Gray code
The complete conversion is, thus (11101) Gray Ans.
Gray–to-binary Conversion.
For conversion of Gray code number into binary code number, the
applicable rules will be as follow:
1. The most significant digit (left-most) in the binary code is the same
as the corresponding digit in Gray code.
2. Add each binary digit generated to Gray digit in next adjacent
position. Discard carries if any.
Example 3: Convert the Gray code number 11011 into the Binary
number.
Solution:
Step 1: the left –most digits is the same
1 1 0 1 1 Gray code
1 Binary code
Step 2: add the last binary digit just generated to the Gray digit in the
next position discard carry.
1 1 0 1 1 Gray code
1 0 Binary code
Step 3: Add the last binary digit generated to the next Gray digit
1 1 0 1 1 Gray code
1 0 0 Binary code
Step 4: Add the last binary digit generated to the next Gray digit
1 1 0 1 1 Gray code
1 0 0 1 Binary code
Step 5: Add the last binary digit generated to the next Gray digit
1 1 0 1 1 Gray code
1 0 0 1 0 Binary code
Thus, conversion is complete and final result is (10010) 2 Ans.
Table 4 shows several possible sets of binary codes for the ten decimal
digits. Many other possibilities exist because the only requirement for a
valid code is that each decimal digit be represented by a distinct
combination of binary digits.
Table 4 Binary Codes for Decimal Digits
HW. L2
1) Construct a table for 7-3-2-1 weighted code and write 3659 using
this code.
2) Construct a table for 4-3-2-1 weighted code and write 9154 using
this code.
3) Construct a 6-2-2-1 weighted code for decimal digits. What
number does 1100 0011 represent in this code?
4) Convert to hexadecimal, and then give the ASCII code for the
resulting hexadecimal number (including the code for the
hexadecimal point):
(a) 222.2210 (b) 183.8110
Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)
14