Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views13 pages

33 Jess Exp4

Uploaded by

jessjohn2209
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)
14 views13 pages

33 Jess Exp4

Uploaded by

jessjohn2209
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/ 13

Don Bosco Institute of Technology, Kurla

Academic Year 2023-24

EXPERIMENT NO. 4

SEMESTER: V DATE OF PERFORMANCE: 02/08/2023

SUBJECT: CN Lab DATE OF SUBMISSION: 10/08/2023

NAME OF THE STUDENT: Jess John ROLL NO.: 33

AIM Write a program to simulate Hamming code generation, detection and


correction.
LEARNING The student will demonstrate the working of (7,4) hamming code.
OBJECTIVE
LEARNING The student will be able to detect and correct errors using hamming code.
OUTCOME

COURSE CSL502.3: Simulate and explore networking algorithms and protocols.


OUTCOME

PROGRAM PO1,PO2,PO3,PO4,PO5,PO9,PO10,PSO1,PSO2,PSO3
OUTCOME
BLOOM'S Apply
TAXONOMY
LEVEL
THEORY Parity bits: The bit which is appended to the original data of binary bits so
that the total number of 1s is even or odd.

Even parity: To check for even parity, if the total number of 1s is even, then
the value of the parity bit is 0. If the total number of 1s occurrences is odd,
then the value of the parity bit is 1.

Odd Parity: To check for odd parity, if the total number of 1s is even, then
the value of parity bit is 1. If the total number of 1s is odd, then the value of
parity bit is 0.

Algorithm of Hamming code:

 An information of 'd' bits are added to the redundant bits 'r' to form
d+r.
 The location of each of the (d+r) digits is assigned a decimal value.
 The 'r' bits are placed in the positions 1,2,.....2k-1.
 At the receiving end, the parity bits are recalculated. The decimal
value of the parity bits determines the position of an error.

LAB EXERCISE Write program in C/C++/Java/Python (Write separate programs for odd
and even parity)

Class: T.E Comps (Sem V) Subject: CN Lab


Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

1. Ask the user whether the program will work for even parity (or for odd)
parity.

2. The user can enter the 4-bit data .


3. Complete Code Word for this and can be generated by calculating for the
parity bits:

P1=(D3,D5,D7)

P2=(D3,D6,D7)

P4=(D5,D6,D7)
4.Enter the Received codeword (with or without error).Notify the user to
introduce error at only one bit position.Note-If error is introduced at more
than one position then display “Hamming code is just 1-bit error detection and
correction mechanism”.

5.Check bits 1,3,5,7.....to generate C1.Check bits 2,3,6,7.....to generate


C2.Check bits 4,5,6,7.....to generate C3.Generate the error codeword
=C3C2C1.

6.If error is there,it will be reflected at which position and will display the
corrected code word by inverting the respective bit.

7.Decode the data bits.


/* Sample Output

This is hamming code error detection and correction using EVEN parity

Enter 4 data bits.D7 D6 D5 D3

Enter the value of D7:1

Enter the value of D6:0

Enter the value of D5:1

Enter the value of D3:0

3 parity bits are required for the transmission of data bits.

SENDER:

The data bits entered are: 1 0 1 0

The Parity bits are:

Class: T.E Comps (Sem V) Subject: CN Lab


Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

Value of P4 is 0

Value of P2 is 1

Value of P1 is 0

The Hamming code is as follows :-

D7 D6 D5 P4 D3 P2 P1

1010010

Enter the hamming code with error at any position of your choice.

NOTE: ENTER A SPACE AFTER EVERY BIT POSITION.

Error should be present only at one bit position

1010110

RECEIVER:

Error is detected at position 3 at the receiving end.

Correcting the error....

The correct code is 1 0 1 0 0 1 0

The decoded data is:1010

*/

Append the code here with all possible sample outputs.

Even Parity:

#include<stdio.h>
#define DSIZE 7
#define PSIZE 3
#define IPSIZE 4

int main(){
int data[DSIZE], ip[IPSIZE], d_error[DSIZE], e_pos, c1, c2,
c3, i, count;
printf("Hamming Code for Even Parity\n");
printf("===========================\n\n");

Class: T.E Comps (Sem V) Subject: CN Lab


Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

printf("Enter data bits:\n");


for(i=0; i<IPSIZE; i++){
scanf("%d", &ip[i]);
}
data[6] = ip[0];
data[5] = ip[1];
data[4] = ip[2];
data[2] = ip[3];

// p4
count = 0;
for(i=4; i<=6; i++){
if(data[i]==1){
count++;
}
}
if(count%2 == 0){
data[3] = 0;
}
else{
data[3] = 1;
}

// p2
count = 0;
for(i=2; i<=6; i++){
if(i == 3 || i == 4){
continue;
}
else{
if(data[i]==1){
count++;
}
}
}
if(count%2 == 0){
data[1] = 0;
}
else{
data[1] = 1;
}

// p1
count = 0;
for(i=2; i<=6; i++){

Class: T.E Comps (Sem V) Subject: CN Lab


Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

if(i == 3 || i == 5){
continue;
}
else{
if(data[i]==1){
count++;
}
}
}
if(count%2 == 0){
data[0] = 0;
}
else{
data[0] = 1;
}

printf("\nParity bits are:\n");


printf("P4 = %d\n", data[3]);
printf("P2 = %d\n", data[1]);
printf("P1 = %d\n", data[0]);

printf("\nThe encoded data is:\n");


for(i=DSIZE-1; i>=0; i--){
printf("%d", data[i]);
}

printf("\n\nEnter the hamming code with 1 bit error:\n");


for(i=DSIZE-1; i>=0; i--){
scanf("%d", &d_error[i]);
}

//RECEIVER

printf("\nRECEIVER:\n\n");

//c1
count = 0;
for(i=0; i<DSIZE; i++){
if(i%2 != 0){
continue;
}
else{
if(d_error[i] == 1){
count++;
}

Class: T.E Comps (Sem V) Subject: CN Lab


Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

}
}
c1 = count%2;

//c2
count = 0;
for(i=0; i<DSIZE; i++){
if(i == 0 || i ==3 || i == 4){
continue;
}
else{
if(d_error[i] == 1){
count++;
}
}
}
c2 = count%2;

//c3
count = 0;
for(i=3; i<DSIZE; i++){
if(d_error[i] == 1){
count++;
}
}
c3 = count%2;

if(c3 == 1){
e_pos +=4;
}

if(c2 == 1){
e_pos +=2;
}

if(c1 == 1){
e_pos +=1;
}

printf("Error is detected at position %d at the receiving


end!\n", e_pos);
printf("Correcting the error...\n\n");

if(d_error[e_pos-1] == 0){
d_error[e_pos-1] = 1;

Class: T.E Comps (Sem V) Subject: CN Lab


Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

}
else{
d_error[e_pos-1] = 0;
}

printf("The corrected code is:\n");


for(i=DSIZE-1; i>=0; i--){
printf("%d", d_error[i]);
}

printf("\nThe decoded data is:\n");


for(i=DSIZE-1; i>=2; i--){
if(i == 3){
continue;
}
printf("%d", d_error[i]);
}

printf("\n");
return 0;
}

Class: T.E Comps (Sem V) Subject: CN Lab


Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

Odd Parity:

#include<stdio.h>
#define DSIZE 7
#define PSIZE 3
#define IPSIZE 4

int main(){
int data[DSIZE], ip[IPSIZE], d_error[DSIZE], e_pos, c1, c2,
c3, i, count;

printf("Hamming Code for Odd Parity\n");


printf("===========================\n\n");
printf("Enter data bits:\n");
for(i=0; i<IPSIZE; i++){
scanf("%d", &ip[i]);
}
data[6] = ip[0];
data[5] = ip[1];
data[4] = ip[2];
data[2] = ip[3];

// p4
count = 0;
for(i=4; i<=6; i++){
if(data[i]==1){
count++;
}
}
if(count%2 == 0){
data[3] = 1;
}
else{
data[3] = 0;
Class: T.E Comps (Sem V) Subject: CN Lab
Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

// p2
count = 0;
for(i=2; i<=6; i++){
if(i == 3 || i == 4){
continue;
}
else{
if(data[i]==1){
count++;
}
}
}
if(count%2 == 0){
data[1] = 1;
}
else{
data[1] = 0;
}

// p1
count = 0;
for(i=2; i<=6; i++){
if(i == 3 || i == 5){
continue;
}
else{
if(data[i]==1){
count++;
}
}
}
if(count%2 == 0){
data[0] = 1;
}
else{
data[0] = 0;
}

printf("\nParity bits are:\n");


printf("P4 = %d\n", data[3]);
printf("P2 = %d\n", data[1]);
printf("P1 = %d\n", data[0]);

Class: T.E Comps (Sem V) Subject: CN Lab


Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

printf("\nThe encoded data is:\n");


for(i=DSIZE-1; i>=0; i--){
printf("%d", data[i]);
}

printf("\n\nEnter the hamming code with 1 bit error:\n");


for(i=DSIZE-1; i>=0; i--){
scanf("%d", &d_error[i]);
}

//RECEIVER

printf("\nRECEIVER:\n\n");

//c1
count = 0;
for(i=0; i<DSIZE; i++){
if(i%2 != 0){
continue;
}
else{
if(d_error[i] == 1){
count++;
}
}
}
if(count%2 == 0){
c1 = 1;
}
else{
c1 = 0;
}

//c2
count = 0;
for(i=0; i<DSIZE; i++){
if(i == 0 || i ==3 || i == 4){
continue;
}
else{
if(d_error[i] == 1){
count++;
}
}
}

Class: T.E Comps (Sem V) Subject: CN Lab


Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

if(count%2 == 0){
c2 = 1;
}
else{
c2 = 0;
}

//c3
count = 0;
for(i=3; i<DSIZE; i++){
if(d_error[i] == 1){
count++;
}
}
if(count%2 == 0){
c3 = 1;
}
else{
c3 = 0;
}

if(c3 == 1){
e_pos +=4;
}

if(c2 == 1){
e_pos +=2;
}

if(c1 == 1){
e_pos +=1;
}

printf("Error is detected at position %d at the receiving


end!\n", e_pos);
printf("Correcting the error...\n\n");

if(d_error[e_pos-1] == 0){
d_error[e_pos-1] = 1;
}
else{
d_error[e_pos-1] = 0;
}

printf("The corrected code is:\n");

Class: T.E Comps (Sem V) Subject: CN Lab


Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

for(i=DSIZE-1; i>=0; i--){


printf("%d", d_error[i]);
}
printf("\nThe decoded data is:\n");
for(i=DSIZE-1; i>=2; i--){
if(i == 3){
continue;
}
printf("%d", d_error[i]);
}

printf("\n");
return 0;
}

Class: T.E Comps (Sem V) Subject: CN Lab


Don Bosco Institute of Technology, Kurla
Academic Year 2023-24

REFERENCES  B.A. Forouzan, “Data Communications and Networking”, TMH,


Fourth Edition.
 https://www.javatpoint.com/computer-network-error-correction

Class: T.E Comps (Sem V) Subject: CN Lab

You might also like