0% found this document useful (0 votes)
26 views

L2 Digital Coding

Uploaded by

xyztypeepytzyx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

L2 Digital Coding

Uploaded by

xyztypeepytzyx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

University of Bahrain Lesson 2 EENG251

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.

Binary Coded Decimal (BCD) Code


The 8-4-2-1 code or simply the BCD code is composed of four bits
representing the decimal digits 0 through 9, so 8, 4, 2 and 1 are the
weights of the four bits (23, 22, 21, 20) Of each decimal digit similar to
straight binary number system. The ease of conversion between 8-4-2-1
code numbers and familiar decimal numbers is the main advantage of this
code.

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


1
University of Bahrain Lesson 2 EENG251

Table 1. BCD code


BCD code Decimal BCD code Decimal
number number
0000 0 0101 5
0001 1 0110 6
0010 2 0111 7
0011 3 1000 8
0100 4 1001 9

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

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


2
University of Bahrain Lesson 2 EENG251

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.

Example: Convert the following numbers in BCD code:-


a- 328. b- 1497.
Solution:
a- 3 2 8
0011 0010 1000
So (328)10 = 0011 0010 1000 BCD Ans.

b- 1 4 9 7
0001 0100 1001 0111
So (1497)10 = 0001 0100 1001 0111 BCD Ans.

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


3
University of Bahrain Lesson 2 EENG251

Example: Convert the following decimal number into


8-4-2-1 equivalents.
99.9 ; 13
Solution: 99.910 = 1001 1001 . 1001 BCD Ans.
(13)10 = 0001 0011

Example: Convert the binary number 1101010 to BCD.


Solution: (110 1010 )2 = 1*26+ 1*25 + 0*24 + 1*23 + 0*22 + 1*21
+0*20 = 10610
and 106 10 = 0001 0000 0110 BCD
So, 110 1010 2 = 0001 0000 0110 BCD Ans.

Example: Convert each of the following 8-4-2-1 BCD numbers into


decimal numbers:
a- 1001 0000 0101 b- 0111 0001 0010
Solution:
a- 1001 0000 0101 = 905
b- 0111 0001 0010 = 712

Example: Add the following BCD numbers


a- 0100 and 0010 b- 0101 0110 and 0011 0010
c- 1001 and 0101 d- 1001 and 1010
e- 0011 0110 and 0001 0101 f- 0111 0111 and 0101 0011
Solution:
a- 0100 b- 0101 0110
+ 0010 + 0011 0010
_______ __________
0110 Ans. 1000 1000 Ans.

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


4
University of Bahrain Lesson 2 EENG251

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.

Decimal BCD 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

As another example, let us convert 26 into its excess3- code


representation.

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


6
University of Bahrain Lesson 2 EENG251

2 6
+ 3 +3
add 3 to each digit
5 9
0101 1001 convert to 4-bit binary code

Since no definite weights can be assigned to the 4 digit positions,


excess-3 is an unweighted code. Excess3- code for decimal digits 0
through 9 are given in Table 2 above. The worth noting point from the
table is that both codes (BCD and excess-3) use only 10 of the 16
possible 4-bit code groups.
The excess-3 code, however, does not use the same cod groups. For
excess-3 code the invalid code groups are 0000 , 0001 , 1101 , 1110 ,
and 1111 .
The key feature of the excess-3 code is that is self-complementing
code. It means that 1's complement of the coded number yields 9's
complement of the number itself.
For example, excess-3 code of decimal 5 is 1000, it's 1's complement
is 111, which is excess-3 code for decimal 4, which is 9's complement
of 5.
It should be noted that the 1's complement is easily produced with
digital logic circuits ... simply inverting each bit.
The self-complementing property makes the excess-3 code useful in
some arithmetic operations, because subtraction can be performed
using the 9's complement method.

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


7
University of Bahrain Lesson 2 EENG251

Example: Encode the decimal number 2345 in BCD and excess-3


cods.
2 3 4 5
BCD 0010 0011 0100 0101 Ans.
+ 0011 +0011 +0011 + 0011 add3
excess-3 code
0101 0110 0111 1000 Ans.

Example: Encode (1236)10 in excess-3 code


1 2 3 6
+ 3 +3 +3 +3
4 5 6 9
excess-3 code
0100 0101 0110 1001 Ans

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.

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


8
University of Bahrain Lesson 2 EENG251

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

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


9
University of Bahrain Lesson 2 EENG251

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.

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


10
University of Bahrain Lesson 2 EENG251

Example 2: Obtain the following conversion:


(111011) 2 to Gray code
Solution:
Step 1: the left –most gray digit is the same as the left most binary digit
1 1 1 0 1 1 Binary code
1 Gray code
Step 2: add the left most binary digit to the adjacent one
1 1 1 0 1 1 Binary code
1 0 Gray code
Step 3: Add the next adjacent binary number.
1 1 1 0 1 1 Binary code
1 0 0 Gray code
Step 4: Add the next adjacent binary number.
1 1 1 0 1 1 Binary code
1 0 0 1 Gray code
Step 5: Add the next adjacent binary number.
1 1 1 0 1 1 Binary code
1 0 0 1 1 Gray code
Step 6: Add the next adjacent binary number.
1 1 1 0 1 1 Binary code
1 0 0 1 1 0 Gray code
The complete conversion is thus (100110) Gray Ans.

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


11
University of Bahrain Lesson 2 EENG251

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

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


12
University of Bahrain Lesson 2 EENG251

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

ASCII code (American Standard Code for Information


Interchange)
Many applications of computers require the processing of data which
contains numbers, letters, and other symbols such as punctuation marks.
In order to transmit such alpha numeric data to or from a computer or

Dr.Mohammed Al-Khalidy EENG251 (Digital Systems I)


13
University of Bahrain Lesson 2 EENG251

store it internally in a computer, each symbol must be represented by a


binary code. One common alphanumeric code is the ASCII code
(American Standard Code for Information Interchange). The word “Start”
is represented in ASCII code as follows:

Table 5 shows a portion of the ASCII code.


Table 5 ASCII code

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy