Chapter - 2
Chapter - 2
Chapter - 2
Ch ter
2 Codes
2.1 Introduction
A code is a symbolic representation of discrete information which includes num-
bers, letters or physical quantities. In other words, a code may be defined as a
representation where numbers, letters or words are represented by a special group
of symbols. This is called encoding.
Alphanumeric Non-binary
1. Non-binary
2. Binary
3. Alphanumeric
For example, A binary string of 8- FACT: Binary numbers were first de-
binary bits can represent any 256 scribed by Pingala, in 100 BC. Pingala
(28 ) possible values. was a mathematician of Indian origin.
Note: To avoid ambiguity in 2421 code, the MSB (i.e. the first 2 in 2421) is set to
zero for the first five numbers and for the next five it is set to 1.
As for e.g. 4 can be represented by 1010 or 0100 in 2421 code, so to remove
ambiguity the above rule should be followed. And the representation of 4 is 0100.
Unpacked BCD
In this type of representation, 1 byte (8bit) is required to store a single decimal
digit.
For example, Decimal digit 5 is stored as 00000101.
Similarly, 59 is stored as 00000101 00001001.
Packed BCD
To store a single decimal digit, a nibble (4bit) is used.
For example, Decimal digit 5 is stored as 0101.
Similarly, 59 is stored as 0101 1001.
Gray code
Also called minimum change codes as
only one bit changes in the code group
when going from one step to the next.
Also called Reflected code. FACT: Gray code is used in
It is a non-weighted code. K- maps (Karnaugh maps)
Not well suited for arithmetic operations,
but finds applications in input/output de-
vices.
Gray-binary conversion
Binary to gray conversion
Let a 4bit binary number b = b3 b2 b1 b0 , and the corresponding gray code,
g = g 3 g2 g1 g0
Then the conversion is given as,
g3 = b3
g2 = b3 b2
g1 = b2 b1
g0 = b1 b0
Example 2.1 Convert the binary number (10100)2 into Gray code.
I Solution
The given binary number is = 10100
b4 b3 b2 b1 b0 = 10100
Codes 2.5
g4 = b4 = 1
g3 = b4 b3 = 1 0 = 1
g2 = b3 b2 = 0 1 = 1
g1 = b2 b1 = 1 0 = 1
g0 = b1 b0 = 0 0 = 0, (the process is also explained below)
Example 2.2 Convert the gray code (11110)2 into binary code.
I Solution
The given binary number is = 11110
g4 g3 g2 g1 g0 = 10100
b4 = g4 = 1
b3 = b4 g3 = 1 1 = 0
b2 = b3 g2 = 0 1 = 1
b1 = b2 g1 = 1 1 = 0
b0 = b1 g0 = 0 0 = 0, (the process is also explained below)
2.6 Digital Electronics, an easy approach to learn
Excess 3 codes
An excess 3 code is obtained by adding 3 to a decimal number.
It is a 4-bit Code.
It is a non-weighted code.
It is a self-complementing code.
I Solution
According to the definition, Excess-3 is obtained by adding 3 to decimal number
and converting it to binary equivalent.
So, Excess-3 of 534 = (534)10 + (3)10 = (537)10 but it is wrong
Actually, Excess-3 is a 4bit code, hence, each position of the decimal number
is added by 3, that is,
5 3 4
+3 +3 +3
8 6 7
So, Excess-3 of (534)10 = (867)10 = (100001100111)2
Even parity
Odd parity
If the total number of 1s is even or odd in the data bit, then the parity bit is decided
accordingly to make the total number of 1s in data stream (including the parity
bit), even for even parity & odd in the case of odd parity
Codes 2.7
For example,
Hamming distance
Hamming distance between two hamming words is defined as number of bits
changed from one code word to another.
For example,
The hamming distance between the hamming codes, H 1 - 1100110 and H2 -
0001111, is 4
Hamming distance between H1 and H2 = 4, as four positions of H1 and H2
are different from each other.
For example,
d9 P 8 d7 d6 d5 P 4 d3 P 2 P 1
Here, P1 , P2 , P4 and P8 are parity bits and the rest are data bits.
Example 2.4 Encode the data bit (0011)2 into 7bit hamming code in even parity
mode.
2.8 Digital Electronics, an easy approach to learn
I Solution
In a 7bit hamming code representation, number of parity bits = 7 4 = 3
Hamming code representation
d7 d6 d5 P 4 d3 P 2 P 1
Here, d3 =1
d5 =1
d6 =0
d7 =0
Determining P1
Example 2.5 Find the error bit if any in the received signal in the odd parity
mode, for the stream 0100111.
I Solution
We know, in odd parity mode, the entire data stream should contain odd number
of 1s but the data stream in the question has even no of 1s. So there is an error
bit,
Codes 2.9
0 1 0 0 1 1 1
D7 D6 D5 P4 D3 P2 P1
EBCDIC code
EBCDIC stands for Extended Binary Coded Decimal Information Code. It is used
in Punched card. It is 12bit code.
BOUDETS code
It is a 5bit code used in telegraphy.
2.10 Digital Electronics, an easy approach to learn
9s complement 1s complement
(2)10 (0010)2
0 000 0 00
1 001 0 01
2 010 0 11 Reflected bit patterns
3 011 0 10
4 100 1 10
5 101 1 11 Mirror line (reflective as
well as unidistant)
6 110 1 01
7 111 1 00
Codes 2.11
Brain teaser
1. If the binary equivalent of the decimal number (25) 10 is (11001)2 , find its,
a) BCD Equivalent
b) BCO (Binary Coded Octal) value
c) BCH (Binary Coded Hexadecimal) value
I Solution
a) As we know BCD is a 4bit code
(2 5)10 BCD equivalent is (00100101)2
| |
. &
(0010) (0101)
b) As we know BCO is a 3bit code
(2 5)8 BCD equivalent is (010101)2
| |
. &
(010) (101)
c) (25)16 = (37)10
Straight Binary Conversion gives BCH equivalent = (100101) 2
Note: As hexadecimal is also a 4bit code, (25)16 will also give same result as
BCD equivalent binary = (100101)2 .
Objective Questions
1. What is the difference between binary coding and binary-coded decimal?
5. Select one of the following statements that best describes the parity method
of error detection.
A. MSB
B. MSB, provided the following sequence of remainders are written in
descending order until the final remainder is achieved.
C. LSB
D. LSB, provided the final remainder is used to replace the original LSB,
which is then moved to the MSB position
Codes 2.13
7. How many BCD code bits and how many straight binary bits would be
required to represent the decimal number 643?
8. Which of the following is the primary advantage of using the BCD code
instead of straight binary coding?
A. Fewer bits are required to represent a decimal number with the BCD
code.
B. The relative ease of converting to and from decimal.
C. BCD codes are easily converted to hexadecimal codes.
D. BCD codes are easily converted to straight binary codes.
9. Assign the proper even parity bit to the code 1100001.
A. 11100001 C. 01100001
B. 1100001 D. 01110101
12. The American Standard Code for Information Interchange (ASCII) uses
how many individual pulses for any given character?
A. 1 B. 2 C. 7 D. 8
1. D 2. D 3. B 4. A 5. A 6. A
7. B 8. B 9. A 10. C 11. A 12. C
Exercises
2.1 Convert the following binary number to gray code
a) (10101)2 b) (11111)2 c) (111001)2
2.3 An odd parity system receivers has the following code group 10110, 11010,
110011, 110101110100 and 1100010101010. If any error, determine which
groups has?