Expt.
No:
Page No.:
Date:
Experiment-3
Aim: Write a program for Hamming Code generation for error detection and correction.
Description:
Hamming code is an error-correcting code used to ensure data accuracy during transmission or storage.
Hamming code detects and corrects the errors that can occur when the data is moved or stored from the
sender to the receiver. This simple and effective method helps improve the reliability of communication
systems and digital storage. It adds extra bits to the original data, allowing the system to detect and correct
single-bit errors.
What is Redundant Bits?
Redundant bits are extra binary bits that are generated and added to the information-carrying bits of data
transfer to ensure that no bits were lost during the data transfer. The number of redundant bits can be
calculated using the following formula: 2r ≥ m + r + 1
Types of Parity Bits:
Even Parity Bit: In the case of even parity, for a given set of bits, the number of 1’s are counted. If that
count is odd, the parity bit value is set to 1, making the total count of occurrences of 1’s an even number.
If the total number of 1’s in a given set of bits is already even, the parity bit’s value is 0.
Odd Parity Bit: In the case of odd parity, for a given set of bits, the number of 1’s are counted. If that
count is even, the parity bit value is set to 1, making the total count of occurrences of 1’s an odd number.
If the total number of 1’s in a given set of bits is already odd, the parity bit’s value is 0.
Algorithm of Hamming Code:
Step 1: Write the bit positions starting from 1 in binary form (1, 10, 11, 100, etc).
Step 2: All the bit positions that are a power of 2 are marked as parity bits (1, 2, 4, 8, etc).
Step 3: All the other bit positions are marked as data bits.
Step 4: Each data bit is included in a unique set of parity bits, as determined its bit position in binary form:
Step 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. Set a parity bit to 0 if the total number of ones in the positions it checks is even.
ADITYA ENGINEERING COLLEGE, SURAMPALEM
2 2 A 9 1 A 6 1
Expt. No:
Page No.:
Date:
Program:
#include <stdio.h>
#include <math.h>
int input[32];
int code[32];
int ham_calc(int,int);
int main()
{
int n,i,p_n = 0,c_l,j,k;
printf("Please enter the length of the Data Word: ");
scanf("%d",&n);
printf("Please enter the Data Word:\n");
for(i=0;i<n;i++)
{
scanf("%d",&input[i]);
ADITYA ENGINEERING COLLEGE, SURAMPALEM
2 2 A 9 1 A 6 1
Expt. No:
Page No.:
Date:
i=0;
while(n>(int)pow(2,i)-(i+1))
{
p_n++;
i++;
}
c_l = p_n + n;
j=k=0;
for(i=0;i<c_l;i++)
{
if(i==((int)pow(2,k)-1))
{
code[i]=0;
k++;
}
else
{
code[i]=input[j];
j++;
}
}
for(i=0;i<p_n;i++)
{
int position = (int)pow(2,i);
int value = ham_calc(position,c_l);
code[position-1]=value;
ADITYA ENGINEERING COLLEGE, SURAMPALEM
2 2 A 9 1 A 6 1
Expt. No:
Page No.:
Date:
}
printf("\nThe calculated Code Word is: ");
for(i=0;i<c_l;i++)
printf("%d",code[i]);
printf("\n");
printf("Please enter the received Code Word:\n");
for(i=0;i<c_l;i++)
scanf("%d",&code[i]);
int error_pos = 0;
for(i=0;i<p_n;i++)
{
int position = (int)pow(2,i);
int value = ham_calc(position,c_l);
if(value != 0)
error_pos+=position;
}
if(error_pos == 1)
printf("The received Code Word is correct.\n");
else
printf("Error at bit position: %d\n",error_pos);
}
int ham_calc(int position,int c_l)
{
int count=0,i,j;
i=position-1;
while(i<c_l)
ADITYA ENGINEERING COLLEGE, SURAMPALEM
2 2 A 9 1 A 6 1
Expt. No:
Page No.:
Date:
{
for(j=i;j<i+position;j++)
{
if(code[j] == 1)
count++;
}
i=i+2*position;
}
if(count%2 == 0)
return 0;
else
return 1;
}
Output:
ADITYA ENGINEERING COLLEGE, SURAMPALEM
2 2 A 9 1 A 6 1