0% found this document useful (0 votes)
6 views6 pages

Data Communication Lab 4

This lab report details the implementation of Hamming Code for even parity checking as part of a Data Communication Lab course at Green University of Bangladesh. The objectives include understanding Hamming code and implementing error detection and correction algorithms. The report outlines the procedure, implementation in C++, and discusses the significance of Hamming codes in digital communication and data storage.

Uploaded by

oliketamim
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)
6 views6 pages

Data Communication Lab 4

This lab report details the implementation of Hamming Code for even parity checking as part of a Data Communication Lab course at Green University of Bangladesh. The objectives include understanding Hamming code and implementing error detection and correction algorithms. The report outlines the procedure, implementation in C++, and discusses the significance of Hamming codes in digital communication and data storage.

Uploaded by

oliketamim
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/ 6

lOMoARcPSD|25028670

Green University of Bangladesh


Department of Computer Science and Engineering (CSE)
Faculty of Science and Engineering
Semester: (Spring, Year: 2025), B.Sc. in CSE (Day)

Lab Report NO # 03
Course Title: Data Communication Lab
Course Code: CSE 308 Section: 223 D3

Lab Experiment Name: Implementation of Hamming Code for Even Parity Checking

Student Details
Name ID
Alik Ahmed Tamim 223002082

Lab Date :
Submission Date : 27-04-2025
Course Teacher’s Name : Rusmita Halim Chaity.

Lab Report Status


Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
lOMoARcPSD|25028670

TITLE OF THE LAB REPORT EXPERIMENT

Implementation of Hamming Code for Even Parity Checking .

OBJECTIVES

• To attain knowledge on the Hamming code and how it works.


• To implement Hamming code error detection and correction algorithms.

PROCEDURE
The Hamming Code is simply the use of extra parity bits to allow the identification of an
error.
1. Write the bit positions starting from 1 in binary form (1, 10, 11, 100, etc).
2. All the bit positions that are a power of 2 are marked as parity bits (1, 2, 4, 8, etc).
3. All the other bit positions are marked as data bits.
4. Each data bit is included in a unique set of parity bits, as determined its bit position in binary
form.
(a) Parity bit 1 covers all the bits positions whose binary representation includes a 1 in the
least significant
position (1, 3, 5, 7, 9, 11, etc ).
(b) Parity bit 2 covers all the bits positions whose binary representation includes a 1 in the
second
position from the least significant bit (2, 3, 6, 7, 10, 11, etc).
(c) Parity bit 4 covers all the bits positions whose binary representation includes a 1 in the
third position
from the least significant bit (4–7, 12–15, 20–23, etc).
(d) Parity bit 8 covers all the bits positions whose binary representation includes a 1 in the
fourth position
from the least significant bit bits (8–15, 24–31, 40–47, etc).
(e) In general, each parity bit covers all bits where the bit-wise AND of the parity position and
the bit
position is non-zero.
5. Since we check for even parity set a parity bit to 1 if the total number of ones in the positions
it checks
is odd.
6. Set a parity bit to 0 if the total number of ones in the positions it checks is even.

IMPLEMENTATION
lOMoARcPSD|25028670

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class hamming{
public:
string data;
int m , r = 0;
char * msg;
hamming(string data){
this->data = data;

reverse(data.begin(),data.end());
m = data.size();
int power = 1;

while(power < (m + r + 1)){


r++;
power*=2;
}
msg = new char[m+r+1];
int curr = 0;

for(int i = 1 ; i <= m+r ; i++){


if(i & (i-1)){
msg[i] = data[curr++];
}
else msg[i] = 'n';
}

setRedundantBits();
}

void showmsg(){
cout << "the data packet to be sent is : ";
for(int i = m+r ; i >= 1 ; i--){
cout << msg[i] << " ";
}
cout << endl;
}
void setRedundantBits(){

int bit = 0;

for(int i = 1 ; i <= m+r ; i*=2){


int count = 0;
lOMoARcPSD|25028670

for(int j = i+1 ; j<=m+r ; j++){

if(j & (1 << bit)){


if(msg[j] == '1') count++;
}
}

if(count & 1) msg[i] = '1';


else msg[i] = '0';

bit++;
}

showmsg();
}
void receiver(){

string ans = "";


int bit = 0;

for(int i = 1 ; i <= m+r ; i*=2){


int count = 0;
for(int j = i+1 ; j<=m+r ; j++){
if(j & (1 << bit)){
if(msg[j] == '1') count++;
}
}

if(count & 1){


if(msg[i] == '1') ans.push_back('0');
else ans.push_back('1');
}
else{
if(msg[i]=='0') ans.push_back('0');
else ans.push_back('1');
}
bit++;
}

if(ans.find('1') != string::npos){
int power = 1;
int wrongbit = 0;

for(int i = 0 ; i < ans.size() ; i++){


if(ans[i]=='1') wrongbit+=power;
power*=2;
lOMoARcPSD|25028670

}
cout << "bit number " << wrongbit << " is wrong and having error " << endl;
}

else{
cout << "correct data packet received " << endl;
}
}
};
int main(){
string data = "1010110";
hamming h(data);

h.receiver();
return 0;
}

OUTPUT

Data-1010110
M (Data Size) : 7
r=4
p1 -1, 3, 5, 7, 9, 11
p2 -2, 3, 6, 7, 10, 11
p3 -4, 5, 6, 7
p4 -8, 9, 10, 11

11 10 9 8 7 6 5 4 3 2 1
1 0 1 0 1 1 0

p1 11 10 9 8 7 6 5 4 3 2 1
1 0 1 0 1 1 0 1
lOMoARcPSD|25028670

P2 11 10 9 8 7 6 5 4 3 2 1
1 0 1 0 1 1 0 0 1

p3 11 10 9 8 7 6 5 4 3 2 1
1 0 1 0 1 1 0 0 0 1

p4 11 10 9 8 7 6 5 4 3 2 1
1 0 1 0 0 1 1 0 0 0 1

DISCUSSION

Hamming codes are a class of error-correcting codes used in digital communication and data
storage to detect and correct errors. Developed by Richard Hamming in the 1950s, they add
extra parity bits to data to ensure accurate transmission. Hamming codes work by inserting
redundant bits at specific positions within a data word. These bits are calculated to check and
correct errors. The receiver can use the parity bits to detect errors, and in some cases, correct
single-bit errors, improving data reliability. Hamming codes are efficient for single-bit error
correction, but they have limitations. They can't correct multiple errors within the same data
word. Despite this, Hamming codes remain a fundamental concept in information theory,
forming the basis for more advanced error-correction codes like Reed-Solomon codes and Turbo
codes, which are used in modern communication systems to ensure robust data transmission.

You might also like