Professional Documents
Culture Documents
Digital Assignment – 1
22MIC0044
DATA COMMUNICATION LAB DA 1
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.
Tasks:
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
Ans:
22MIC0044
DATA COMMUNICATION LAB DA 1
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
}
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
// Introduce an error
hammingCode[2] ^= 1;
return 0;
}
COMPLETE CODE WITH OUTPUT:
22MIC0044
DATA COMMUNICATION LAB DA 1
22MIC0044
DATA COMMUNICATION LAB DA 1
22MIC0044
DATA COMMUNICATION LAB DA 1