DCIT 102
FUNDAMENTALS OF HARDWARE
AND CIRCUIT
SESSION 5: INTRODUCTION TO CODES
Dwumfour Abdullai Aziz, Dept of Computer Sc.
Contact Information:
[email protected]UNIVERSITY OF GHANA
College
CollegeofofBasic and Applied
Education
School
Schoolof
ofPhysical andand
Continuing Mathematical Sciences
Distance Education
2021/2022
2014/2015 – 2016/2017
SESSION OVERVIEW
Numbers or letters are represented by a specific group of
symbols called codes.
The digital data is represented, stored and transmitted as
group of bits binary code.
This section will introduce students the various types of
codes used to represent digital data.
Dwumfour Abdullai Aziz Slide 2
SESSION OUTLINE
Key topics covered in this session include;
• Weighted code
• Unweighted code
• 8421 Codes
• 2421 Codes
• 5211 Codes
• Reflective code
• Sequential code
• Excess-3 Codes
• Gray Codes
Dwumfour Abdullai Aziz Slide 3
SESSION OUTLINE
Stallings, William (2019). Computer Organization and
Architecture: Designing for Performance, Eleventh edition.
Chapter 10
Dwumfour Abdullai Aziz Slide 4
CODES
What is binary code?
• Binary code is any data, text, or computer instructions
represented using a two-symbol system.
• These two numeral symbols are 0 and 1
• Computers and digital electronic devices can only
communicate using 0’s and 1’s
• In this session, you will learn about weighted code, non-
weighted code, BCD code, Excess-3 code, gray code,
alphanumeric code, and error detection codes using parity
method.
Dwumfour Abdullai Aziz Slide 5
Classification of Binary Code
The following are binary code classification;
q Weighted binary code
q Non-weighted binary code
q Alphanumeric code
q Error detection code
q Error correction code
Dwumfour Abdullai Aziz Slide 6
Classification of Binary Code
Weighted binary code
• Weighted binary code is a binary code that obeys the
positional weight system.
• This is a system where every digit is assigned a specific
weight based on its position.
• Binary weights are values assigned to binary numbers based
on their position in binary code. They help to convert binary
numbers to decimal numbers easily.
Dwumfour Abdullai Aziz Slide 7
Weighted Code
Example of weighted code computation
• To convert 10110 to decimal system, consider the weights of
this binary code; 16 , 8 , 4 , 2 , 1
• These values originate from 2 to the power of their binary
position: 24 , 2³ , 2² , 2¹ , 2⁰ , starting from the most significant
bit (MSB) to the least significant bit (LSB).
Dwumfour Abdullai Aziz Slide 8
Weighted Binary Systems
• The values assigned to consecutive places in the decimal
system which is a place value system are 10⁴, 10³, 10², 10¹,
10⁰, 10⁻¹, 10⁻², 10⁻³… and so on from left to right.
• It is easily can be understood that the weight of digit of the
decimal system is ‘10’.
• For example:
• (3546.25)10 = 3 x 10³ + 5 x 10² + 4 x 10¹ + 6 x 10⁰ + 2 x 10⁻¹
+ 5 x 10⁻²
Dwumfour Abdullai Aziz Slide 9
Types of weighted code
Most common types of weighted code include
• Binary Coded Decimal (BCD)
– BCD code is an example of a weighted binary code. It is
also known as 8421 code
– Binary Coded Decimal code allows us to convert
decimal numbers into their binary equivalents easily
– Each BCD digit is presented by 4-bit binary number
• 2421 and 5211 are also weighted codes
Dwumfour Abdullai Aziz Slide 10
8421 Code or BCD Code
DECIMAL NUMBER BINARY NUMBER 4 BIT EXPRESSION(8421)
0 0 0000
1 1 0001
2 10 0010
3 11 0011
4 100 0100
5 101 0101
6 110 0110
7 111 0111
8 1000 1000
9 1001 1001
Dwumfour Abdullai Aziz Slide 11
BCD –Binary Coded Decimal
Example
• Let us find the BCD equivalent of the decimal number 543.
This number has 3 decimal digits 5, 4 and 3.
• BCD 8421 codes of 5, 4 and 3 are 0101, 0100 and 0011
respectively.
• Joining all the binary numbers obtained, we have
• 54310 = 010101000011BCD
Dwumfour Abdullai Aziz Slide 12
2421 - Code
• The weights of this code are 2, 4, 2 and 1.
• This code has all positive weights.
• it is a positively weighted code.
• It is an unnatural BCD code; Sum of weights of unnatural
BCD codes is equal to 9.
• It is a self-complementing code.
• Self-complementing codes provide the 9’s complement of
a decimal number, just by interchanging 1’s and 0’s in its
equivalent 2421 representation.
Dwumfour Abdullai Aziz Slide 13
2 421– Code
Example
• Let us find the 2421 equivalent of the decimal number 786.
• This number has 3 decimal digits 7, 8 and 6.
• we can write the 2421 codes of 7, 8 and 6 are 1101, 1110 and
1100 respectively.
• Therefore, the 2421 equivalent of the decimal number 786
is 110111101100.
Dwumfour Abdullai Aziz Slide 14
8421 Code or BCD Code
• As per the above expression all the decimal numbers written in
the 4 Bit binary code in the form of 8421 and this is called as
8421 Code and also as Binary coded decimal BCD.
• As this is a straight code, any Decimal number can be expressed
easily because the weights of the positions are straight for easy
conversion into this 8421 code.
• There are other forms of codes which are not so popular but
rather confusing. They are 2421 code, 5211 code, reflective code,
sequential code, non- weighted coded, excess-3 code and Grey
code. They are having their own importance for some of the
exclusive applications and may be useful for some of the special
applications.
Dwumfour Abdullai Aziz Slide 15
2421 Code
This code also a 4-bit application code where the binary weights
carry 2, 4, 2, 1 from left to right.
DECIMAL NUMBER BINARY NUMBER 2421 CODE
0 0 0000
1 1 0001
2 10 0010
3 11 0011
4 100 0100
5 101 1011
6 110 1100
7 111 1101
8 1000 1110
9 1001 1111
Dwumfour Abdullai Aziz Slide 16
5211 Code
This code is also a 4-bit application code where the binary weights carry
5, 2, 1, 1 from left to right.
DECIMAL NUMBER BINARY NUMBER 5211 CODE
0 0 0000
1 1 0001
2 10 0011
3 11 0101
4 100 0111
5 101 1000
6 110 1010
7 111 1100
8 1000 1110
9 1001 1111
Dwumfour Abdullai Aziz Slide 17
Reflective Code
It can be observed that in the 2421 and 5211 codes,
the code for decimal 9 is the complement of the code for decimal 0,
the code for decimal 8 is the complement of the code for decimal 1,
the code for decimal 7 is the complement of the code for decimal 2,
the code for decimal 6 is the complement of the code for decimal 3,
the code for decimal 5 is the complement of the code for decimal 4.
These codes are called as Reflective Codes.
The same can be observed in the following table:
Dwumfour Abdullai Aziz Slide 18
Reflective Code
DECIMAL NUMBER BINARY NUMBER 2421 CODE 5211 CODE
0 0 0000 0000
1 1 0001 0001
2 10 0010 0011
3 11 0011 0101
4 100 0100 0111
5 101 1011 1000
6 110 1100 1010
7 111 1101 1100
8 1000 1110 1110
9 1001 1111 1111
Dwumfour Abdullai Aziz NOTE: 8421 code is not a reflective code.
Sequential Codes
• Sequential codes are the codes in which 2 subsequent
numbers in binary representation differ by only one digit.
• The 8421 and Excess-3 codes are examples of sequential
codes.
• 2421 and 5211 codes do not come under sequential codes.
Dwumfour Abdullai Aziz Slide 20
Sequential Codes
DECIMAL NUMBER BINARY NUMBER 8421 CODE EXCESS-3
0 0 0000 0011
1 1 0001 0100
2 10 0010 0101
3 11 0011 0110
4 100 0100 0111
5 101 0101 1000
6 110 0110 1001
7 111 0111 1010
8 1000 1000 1011
9 1001 1001 1100
Dwumfour Abdullai Aziz Slide 21
Non-Weighted Codes
In this type of binary codes, the positional weights are not
assigned.
The examples of non-weighted codes are
• Excess-3 code
• Gray code.
Dwumfour Abdullai Aziz Slide 22
Excess-3 Code
As mentioned above, some of the codes will not follow the binary
weights, Excess-3 code is an example of it and it is an important 4 bit
code.
The excess – 3 code of a decimal number is achieved by adding the
number 3 to the 8421 code. For example to convert 15 to an excess-3
code, first 3 to be added to each digit as shown below.
Dwumfour Abdullai Aziz Slide 23
Excess – 3 Code Examples
1. Find the excess-3 code of (237.75)10
2. Find the decimal number of excess-3 number 110010100011.01110101.
Solution:
• The excess-3 code for (237)10 is obtained by adding 3 to all the
digits individually, that is 2, 3 and 7 will become 5, 6 and 10
respectively.
• These 5, 6 and 10 decimals have to be converted into binary form
and the result is 010101101010.
• The excess-3 code for (.75)10 is obtained by replacing 7 and 5 with
10 and 8 respectively by adding 3 to each digit. That is, the excess-
3 code for (.75)10 is .10101000.
• Combining the results of the integral and fractional parts, the
excess-3 code for (237.75)10 is 010101101010.10101000.
Dwumfour Abdullai Aziz Slide 24
Excess – 3 Code Examples
2. Find the decimal number of excess-3 number
110010100011.01110101.
Solution
The excess-3 code is 110010100011.01110101
By separating 4 bits as group the equivalent excess-3 code is given
as 1100 1010 0011.0111 0101.
Subtracting 0011 from each four-bit group, we obtain the new
number as: 1001 0111 0000.0100 0010.
Therefore, the decimal equivalent is (970.42)10.
Dwumfour Abdullai Aziz Slide 25
Gray Code
• The Gray code is the code where one bit will be differed to the
preceding number.
• For example, decimal numbers 13 and 14 are represented by
Gray code numbers 1011 and 1001, these numbers differ only
in single position that is the second position from the right.
• In the same way first position on the left changes for 7 and 8
which are 0100 and 1100 and this is also called Unit-distance
code.
• The Gray code has very special place in digital electronics.
Dwumfour Abdullai Aziz Slide 26
Gray Code
DECIMAL NUMBER BINARY CODE GRAY CODE
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
Dwumfour Abdullai Aziz15 1111 Slide 27 1000