You are on page 1of 8

22MIC0044

DATA COMMUNICATION LAB DA 1

NAME : ACHYUT SHIVAM

REG. NO.: 22MIC0044

BRANCH: M.TECH INTEGRATED IN COMPUTER SCIENCE

COURSE: DATA COMMUNICATION AND NETWORK

FACULTY: DR. PRAMOD KUMAR MOURYA

Digital Assignment – 1
22MIC0044
DATA COMMUNICATION LAB DA 1

Programme Name: M.Tech. (Integrated)

Course Name & Code: Data Communication and Networks(CSI2007)

Maximum Marks: 10 Instructions:

Instructions:

1. Save the file in the following format (ONLY in pdf format) YourRegNo.pdf (example:
20BCE1234.pdf)
2. Along with code, attach the screenshot only for the output.
3. Plagiarized code from Internet, other sources or fellow-students will lead to award of 0
marks.
4. For implementation, All programming languages are allowed.

Understanding Hamming Code: Error Detection and Correction in Digital Communication

Tasks:

Q1. Provide a brief overview of Hamming code,


highlighting its importance in error detection and
correction.

Ans:
Hamming code is like this cool code thingy that helps fix mistakes when we
send messages. It's super helpful because sometimes when we send messages,
they get all messed up, like if you're texting your friend and the words get
jumbled. Hamming code adds some extra bits to the message, kind of like
backup bits, so if there's a mistake, we can figure out what it's supposed to say
and fix it! It's kind of like having a spell checker for your text messages, but
for computers and stuff. So, yeah, Hamming code is pretty neat because it
helps make sure our messages get through without mistakes!

C CODE

Q2.Implement a function to generate Hamming code for a given


input data sequence.

Ans:
22MIC0044
DATA COMMUNICATION LAB DA 1

void generateHammingCode(int *data, int dataLength, int *hammingCode) {


int m = calculateParityBits(dataLength); int n = dataLength + m;
int i,
j, k;
for (i = 0, j = 0, k = 1; i < n; i++) {
if ((i+1) == (1 << j)) {
hammingCode[i] = 0;
j++;
} else {
hammingCode[i] = data[k-1];
k++;
}
}

for (i = 0, j = 0; i < n; i++) {


if ((i+1) == (1 << j)) {
int count = 0; for (k =
i; k < n; k++) {
if (((k+1) & (1 << j)) != 0 && hammingCode[k] == 1) {
count++;
}
}
hammingCode[i] = (count % 2 == 0) ? 0 : 1;
j++;
}
}
}

Q3.Implement a function to detect errors in a received Hamming


code.

Ans:
bool detectError(int *hammingCode, int codeLength) {
int m = calculateParityBits(codeLength);
int i, j;
for (i = 0; i < m; i++) {
int parity = 0;
for (j = (1 << i) - 1; j < codeLength; j += (1 << (i + 1))) {
int end = j + (1 << i);
for (; j < end && j < codeLength; j++) {
parity ^= hammingCode[j];
}
}
if (parity != hammingCode[(1 << i) - 1]) {
return true; // Error detected
22MIC0044
DATA COMMUNICATION LAB DA 1

}
}
return false; // No error detected
}

Q4. Implement a function to correct single-bit errors in a received


Hamming code.

Ans:
void correctError(int *hammingCode, int codeLength) {
int m = calculateParityBits(codeLength);
int errorIndex = 0;
int i, j;
for (i = 0; i < m; i++) {
int parity = 0;
for (j = (1 << i) - 1; j < codeLength; j += (1 << (i + 1))) {
int end = j + (1 << i);
for (; j < end && j < codeLength; j++) {
parity ^= hammingCode[j];
}
}
if (parity != hammingCode[(1 << i) - 1]) {
errorIndex += (1 << i);
}
}
if (errorIndex > 0) {
hammingCode[errorIndex - 1] ^= 1;
}
}

Q5.Test the implemented functions with sample data and verify their
correctness.

Ans:
int main() {
int data[] = {1, 0, 1, 1}; // Input data
int dataLength = sizeof(data) / sizeof(data[0]);
int m = calculateParityBits(dataLength);
22MIC0044
DATA COMMUNICATION LAB DA 1

int n = dataLength + m;
int hammingCode[n]; // Hamming code

// Generate Hamming code


generateHammingCode(data, dataLength, hammingCode);

printf("Hamming Code: ");


printHammingCode(hammingCode, n);

// Introduce an error
hammingCode[2] ^= 1;

// Detect and correct error


if (detectError(hammingCode, n)) {
printf("Error detected in received Hamming code.\n");
correctError(hammingCode, n);
printf("Corrected Hamming Code: ");
printHammingCode(hammingCode, n);
} else {
printf("No error detected in received Hamming code.\n");
}

return 0;
}
COMPLETE CODE WITH OUTPUT:
22MIC0044
DATA COMMUNICATION LAB DA 1
22MIC0044
DATA COMMUNICATION LAB DA 1
22MIC0044
DATA COMMUNICATION LAB DA 1

You might also like