NAME: ZOHAIB IMAM
BRANCH: MCA
SECTION: 1-E
ROLL NO: 2370527
ASSIGNMENT (COA)
(CODE COVERSION: 1) BINARY TO BCD
2) BCD TO BINARY
3) BCD TO GRAY CODE
4) GRAY CODE TO BINARY
5) HAMMING CODE FOR ERROR DETECTION)
CODE CONVERSION:
1) Convert binary into BCD.
BCD code plays an important role in digital circuits. The BCD stands for
Binary Coded Decimal Number. In BCD code, each digit of the decimal
number is represented as its equivalent binary number. So, the LSB and MSB
of the decimal numbers are represented as its binary numbers.
There are the following steps to convert the binary number to BCD:
First, we will convert the binary number into decimal.
We will convert the decimal number into BCD.
(10110) 2
Answer:
(10110) 2
Converting (10110) into decimal we have,
2
1*2power (4) +0*2power (3) +1*2power (2) +1*2power (1) +0*2 power
(0) =16+0+4+2+0=22
Now 22 is our decimal number.
To convert in BCD, we write each digit of the decimal in 4 bits,
Therefore now 22 will be converted into BCD;
2 = 0010
22 = 0010 0010
So, (10110) 2 = 0010 0010 in BCD.
2) Convert the BCD to Binary Number.
The process of converting BCD code into Binary is opposite to the process
of converting Binary code into BCD There are the following steps to convert
the BCD code into Binary.
In the first step, we will convert the BCD the number into a decimal by
making four- bit groups and finding the equivalent decimal Dumber for each
group.
In the last step, we will convert a decimal number into Binary using the
process of converting decimal to binary number.
100101101000BCD
Answer:
Converting BCD to DECIMAL,
1001 0110 1000
9 6 8
= (968)10
968 / 2 = 484 with 0 remainder
484 / 2 = 242 with 0 remainder
242 / 2 = 121 with 0 remainder
121 / 2 = 60 with 1 remainder
60 / 2 = 30 with 0 remainder
30 / 2 = 15 with 0 remainder
15 / 2 = 7 with 1 remainder
7/2=3 with 1 remainder
3/2=1 with 1 remainder
1/2=0 with 1 remainder
100101101000BCD = 11110010002
3) Convert binary into GRAY CODE.
(11001) 2
Answer:
To convert binary to GRAY CODE,
We take most significant value of binary digit as it is then we perform XOR
operation in between every bit of the binary number.
1 (1 XOR 1) (1 XOR 0) (0 XOR 0) (0 XOR 1)
= 10101
So (11001) in GRAY CODE IS 10101.
2
4) Convert GRAY CODE to Binary.
The Gray to Binary conversion method uses the working concept of the
EX-OR among the bits of gray as well as binary bits.
Gray Code: g(3) g(2) g(1) g(0) = 1001
Binary Code: b(3) b(2) (b1) b(0)
Gray Code: g(3) g(2) g(1) g(0)
Binary Code: b(3) b(2) b(1) b(0)
b(3) = g(3) = 1
b(2) =b(3) XOR g(2) = 1 XOR 0 = 1
b(1) =b(2) XOR g(1) = 1 XOR 0 = 1
b(0) =b1 XOR g(0) = 1 XOR 1 = 0
Therefor, Binary Code = 1110
5) HAMMING CODE for Error detection.
-The Hamming Code method is one of the most effective ways to detect single-
data bit errors in the original data at the receiver end. It is not only used for
error detection but is also for correcting errors in the data bit.
IMPORTANT TERMS FOR HAMMING CODE:
1. Redundant Bits - These are the extra binary bits added externally into the
original data bit to prevent damage to the transmitted data and are also
needed to recover the original data.
The expression applied to deduce the redundant value is,
2r >= d+r+1
Where,
d - “Data Bits”
r - “Redundant Bits”, r = {1, 2, 3, ……. n}
Example: Assuming the number of data bits is 7, find the number of redundant
bits.
2^r >= r+7+1
2^4 >=4+8 [r=4]
The number of redundant bits = 4.
2. Parity Bits - The parity bit is the method to append binary bits to ensure that
the total count of 1’s in the original data is even bit or odd. It is also applied to
detect errors on the receiver side and correct them.
Types of parity bits:
Odd Parity bits - In this parity type, the total number of 1’s in the data
bit should be odd in count, then the parity value is 0, and the value is
1.
Even Parity bits - In this parity type, the total number of 1’s in the
data bit should be even in count; then the parity value is 0, and the
value is 1.
WORKING OF HAMMING CODE:
To solve the data bit issue with the hamming code method, some steps need
to be followed:
Step 1 - The position of the data bits and the number of redundant
bits in the original data. The number of redundant bits is deduced
from the expression [2^r >= d+r+1].
Step 2 - Fill in the data bits and redundant bit, and find the parity bit
value using the expression [2^p, where, p - {0,1,2, …… n}].
Step 3 - Fill the parity bit obtained in the original data and transmit
the data to the receiver side.
Step 4 - Check the received data using the parity bit and detect any
error in the data, and in case damage is present, use the parity bit
value to correct the error.
Q.) Encode a binary word 11001 into the even parity hamming code and
perform the error detection.
ANSWER:
Given, number of data bits, n =5.
To find the number of redundant bits:
Let P=4
The equation is satisfied and so 4 redundant bits are selected.
So, total code bit = n+P = 9
The redundant bits are placed at bit positions 1, 2, 4 and 8.
Constructing the bit location table: -
Bit location 9 8 7 6 5 4 3 2 1
Bit D5 P4 D4 D3 D2 P3 D1 P2 P1
designation
Binary 1001 1000 0111 0110 0101 0100 0011 0010 0001
representation
Information 1 1 0 0 1
bits
Parity bits 1 1 0 1
To determine the parity bits
For P1: Bit locations 3, 5, 7 and 9 have three 1s. To have even parity, P1 must
be 1.
For P2: Bit locations 3, 6, 7 have two 1s. To have even parity, P2 must be 0.
For P3: Bit locations 5, 6, 7 have one 1s. To have even parity, P3 must be 1.
For P4: Bit locations 8, 9 have one 1s. To have even parity, P2 must be 1.
Thus, the encoded 9-bit hamming code is 111001101.
To detect the error:
Bit location 9 8 7 6 5 4 3 2 1
Bit D5 P4 D4 D3 D2 P3 D1 P2 P1
designation
Binary 1001 1000 0111 0110 0101 0100 0011 0010 0001
representation
Received Code 1 1 0 0 0 1 1 0 1
Checking the parity bits
For P1: Check the locations 1, 3, 5, 7, 9. There is three 1s in this group, which is
wrong for even parity. Hence the bit value for P1 is 1.
For P2: Check the locations 2, 3, 6, 7. There is one 1 in this group, which is
wrong for even parity. Hence the bit value for P2 is 1.
For P3: Check the locations 3, 5, 6, 7. There is one 1 in this group, which is
wrong for even parity. Hence the bit value for P3 is 1.
For P4: Check the locations 8, 9. There are two 1s in this group, which is correct
for even parity. Hence the bit value for P4 is 0.
The resultant binary word is 0111. It corresponds to the bit location 7 in the
above table. The error is detected in the data bit D4. The error is 0 and it
should be changed to 1.
Thus, the corrected code is 111001101.