Sunteți pe pagina 1din 14

ap

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.

2.2 Classification of Codes


Code

Alphanumeric Non-binary

e.g. e.g. Morse code


ASCII CODE Binary
EBCDIC
Hollerith code
Boudets code

Weighted code Error detecting and Non-weighted code


correcting code
e.g. e.g.
BCD code or 8421 e.g. Gray code
code Excess - 3 code
Hamming code
2421 code
5421 code
5211 code

Fig. 2.1 Classification of codes


2.2 Digital Electronics, an easy approach to learn

Codes are broadly classified into three groups

1. Non-binary
2. Binary
3. Alphanumeric

2.3 Non-Binary Code


The code which does not use 1s and 0s is called non-binary-code.

2.3.1 Morse code


It is a non-binary code, which uses a series of dots and dashes to represent alpha-
bets, numerical special characters.
For example,
Table 2.1 Conversion of alphabets to Morse code
Alphabets Morse code
Salim ... .- .-..
Rakesh .-. .- -.- . ... ....

2.4 Binary Code


A binary code is a way of representing text or instruction by the use of the binary
number systems two binary digits 0 and 1.

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.

2.4.1 Weighted code


In this code, each bit position is assigned a specific weight.
For example, 8421(BCD), 5421 etc.
In 8421 code, the LSB(0th position) has a weightage of 20 = 1, and MSB(3rd
position) has a weightage of 23 = 8.
Note: Binary equivalent of a given decimal number is not equivalent to its BCD
value. As for example - (25)10 has a binary equivalent of (11001)2 , whereas its
BCD equivalent is (00100101)2 .
Codes 2.3

Table 2.2 Decimal equivalents in different types of codes


Decimal number 8421(BCD) 5421 2421 8421
0 0000 0000 0000 0000
1 0001 0001 0001 0111
2 0010 0010 0010 0110
3 0011 0100 0011 0101
4 0100 0100 0100 0100
5 0101 1000 1011 1011
6 0110 1001 1100 1010
7 0111 1010 1101 1001
8 1000 1011 1110 1000
9 1001 1100 1111 1111

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.

2.4.2 Types of BCD representation


Unpacked BCD
Packed BCD

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.

Representation of negative number in unpacked BCD form


6 can be represented as,
2.4 Digital Electronics, an easy approach to learn

(6)10 (1, 0110)2


The signed bit is converted into a nibble, containing all 1s for negative num-
bers. Hence, corresponding unpacked BCD is (1111 0110).

2.4.3 Non-weighted code


In this code, no specific weight is assigned to any position. This means that no
fixed value is assigned to the bit positions in a binary number.
For example, Gray Code and Excess-3 Code

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)

So, required gray code (g4 g3 g2 g1 g0 ) = (11110)2

Gray to binary conversion


Let a 4bit gray code is represented as g = g3 g2 g1 g0 .
Then the corresponding Binary code, b = b3 b2 b1 b0 .
Where b3 = g3
b2 = b3 g2
b1 = b1 g1
b0 = b1 g0

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

So, the required binary code (b4 b3 b2 b1 b0 ) = (10100)2

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.

Example 2.3 Convert (534)10 into its Excess 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

2.4.4 Parity bit


A parity bit is a bit, that is added to the data bit for the purpose of error detection
and error correction.
There are two types of parity

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,

7 bits of data (count of 1 bits) 8 bits including parity


even odd
0000000 0 00000000 00000001
1010001 3 10100011 10100010
1101001 4 11010010 11010011
1111111 7 11111111 11111110

2.4.5 Error detecting and correcting code


In order to detect and correct errors, R.W. Hamming developed a methodical way
to add one or more parity bit to a data character. So Hamming codes are used for
detecting 1bit error and also correcting 1bit error.
Hamming code = Data bit + parity bit

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.

Rules for representing a hamming code


The hamming code consists of data bit and parity bit.
The position of parity bit is determined by 2n, where n = 0, 1, 2, 3....
The rest positions are data bit.
The MSB must be a data bit.

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

(P1 d3 d5 d7 ) = 0 { even parity is given}


Starts from P1
(P1 1 1 0) = 0 only when
and take 1 , leave 1
P1 = 0
Determining P2

(P2 d3 d6 d7 ) = 0 { even parity is given}


Starts from P2
(P2 1 0 0) = 0 only when
and take -2, leave -2
P2 = 1
Determining P3

(P4 d5 d6 d7 ) = 0 { even parity is given}


Starts from P4
(P4 1 0 0) = 0 only when
and take 4, leave 4
P4 = 1

So, encoded hamming code is (0011110)2 .

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

For odd parity the total number of 1s should be odd.

The error bit is given by (z y x)2 .


So, position of the error is (0 0 1)2 = 1st bit is error.
So corrected code is 0 1 0 0 1 1 0.

2.5 Alphanumeric Code


A code that can be represented by 10 decimal digits, the 26 letters of alphabet &
a certain number of special symbols are called alphanumeric codes.
For example, ASCII code, EBCDIC code, Hollerith code.
Therefore the total number of elements on the alphanumeric group is greater
than 36 so it must be coded with a minimum of 6bits.
ASCII code - ASCII stands for American Standard Code for Information In-
terchange. The ASCII code represents a character with 7bits which can be stored
in 1 byte with one bit unused.
For example, A is coded as 1 0 0 0 0 0 0 1

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

2.6 Sequential Code


In this type of code each succeeding code word, is one binary number greater than
its preceding one.
For example, 8421, excess-3 etc.

2.7 Self Complementary Code


(7)10 1101

9s complement 1s complement

(2)10 (0010)2

If for a given code, the 9s complement in decimal is same as the 1s complement


in the binary then it is called self complementary code.
For example, 2421, excess-3, 8421, 5211, etc.
(2)10 = (0010)2 . So, 2421 is self complementing code.
Note: If sum of all the weightage in the code is 9, then that code is self-complementary
2 + 4 + 2 + 1 = 9. i.e. 2421 is self-complementary.

2.8 Reflective Code


A code that reflects the bit patterns of its character about its mid-point is called
Reflective code.
For example, Gray code.
Table 2.3 Binary and gray codes

Decimal Binary Gray code

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

2.9 Cyclic Code


Cyclic codes are those in which each succeeding code word differ from the pre-
ceding one by 1bit position only.
e.g. Gray code.

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?

A. BCD is pure binary.


B. Binary coding has a decimal format.
C. BCD has no decimal format.
D. Binary coding is pure binary.
2.12 Digital Electronics, an easy approach to learn

2. Convert the Gray code 1011 to binary.

A. 1011 B. 1010 C. 0100 D. 1101

3. Convert the BCD number 100000000011 to decimal.


A. 8003 B. 803 C. 1003 D. 103

4. The binary-coded decimal (BCD) system can be used to represent each of


the 10 decimal digits as a(n):

A. 4bit binary code C. 16bit binary code


B. 8bit binary code D. ASCII code

5. Select one of the following statements that best describes the parity method
of error detection.

A. Parity checking is best suited for detecting single-bit errors in trans-


mitted codes.
B. Parity checking is not suitable for detecting single-bit errors in trans-
mitted codes.
C. Parity checking is capable of detecting and correcting errors in trans-
mitted codes.
D. Parity checking is best suited for detecting double-bit errors that occur
during the transmission of codes from one location to another.

6. When using the repeated division by 2 method of converting from decimal


to binary, one must write the first remainder as

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?

A. 12 BCD, 12 binary C. 12 BCD, 9 binary


B. 12 BCD, 10 binary D. 16 BCD, 9 binary

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

10. Alphanumeric codes should include as a minimum:

A. The capacity to represent the alphabet upper and lowercase characters


and the decimal numbers in a straight binary format.
B. The capacity to code all possible decimal numbers in a direct octal
representation of BCD codes.
C. The alphabet upper and lowercase letters, the decimal digits, the seven
punctuation marks, and other characters or symbols.
D. The ability to represent decimal numbers greater than 12810 in a straight
binary format.

11. The BCD number for decimal 16 is .


A. 00010110 C. 00010010
B. 00010000 D. 11100000
2.14 Digital Electronics, an easy approach to learn

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

Answers for objectives

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.2 Convert the following gray codes to binary


a) (11111)2 b) (10000)2 c) (100101)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?

Answers for exercises

2.1 a) 11111 b) 10000 c) 100101

2.2 a) 10101 b) 111111 c) 11101

2.3 110011, 1100010101010

S-ar putea să vă placă și