Hamming Code in C and C++

Here you will get program for hamming code in C and C++.

Hamming code is a popular error detection and error correction method in data communication. Hamming code can only detect 2 bit error and correct a single bit error which means it is unable to correct burst errors if may occur while transmission of data.

Also Read: Checksum Program in C and C++

Hamming code uses redundant bits (extra bits) which are calculated according to the below formula:-

2r ≥ m+r+1

Where r is the number of redundant bits required and m is the number of data bits.

R is calculated by putting r = 1, 2, 3 … until the above equation becomes true.

R1 bit is appended at position 20

R2 bit is appended at position 21

R3 bit is appended at position 22 and so on.

These redundant bits are then added to the original data for the calculation of error at receiver’s end.

At receiver’s end with the help of even parity (generally) the erroneous bit position is identified and since data is in binary we take complement of the erroneous bit position to correct received data.

Respective index parity is calculated for r1, r2, r3, r4 and so on.

Hamming Code in C and C++

Image Source

Advantages of Hamming Code

  1. Easy to encode and decode data at both sender and receiver end.
  2. Easy to implement.

Disadvantages of Hamming Code

  1. Cannot correct burst errors.
  2. Redundant bits are also sent with the data therefore it requires more bandwidth to send the data.

Program for Hamming Code in C

Program for Hamming Code in C++

Output

Enter 4 bits of data one by one
1
0
1
0

Encoded data is
1010010

Enter received data bits one by one
1
0
1
0
0
1
0

No error while transmission of data

Code Source: http://scanftree.com/programs/c/implementation-of-hamming-code/

This article is submitted by Rahul Maheshwari. You can connect with him on facebook.

Comment below if you have any queries related to above hamming code program in C and C++.

5 thoughts on “Hamming Code in C and C++

  1. kd7

    This is the correct code please refer this:—-

    #include
    #include
    void main() {
    int data[10];
    int dataatrec[10],c,c1,c2,c3,i;
    clrscr();
    printf(“Enter 4 bits of data one by one\n”);
    scanf(“%d”,&data[0]);
    scanf(“%d”,&data[1]);
    scanf(“%d”,&data[2]);
    scanf(“%d”,&data[4]);

    data[6]=data[0]^data[2]^data[4];
    data[5]=data[0]^data[1]^data[4];
    data[3]=data[0]^data[1]^data[2];

    printf(“\nEncoded data is\n”);
    for(i=0;i<7;i++)
    printf("%d",data[i]);

    printf("\n\nEnter received data bits one by one\n");
    for(i=0;i<7;i++)
    scanf("%d",&dataatrec[i]);

    c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0];
    c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];
    c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];
    c=c3*4+c2*2+c1 ;

    if(c==0 && dataatrec[6]==data[6] && dataatrec[5]==data[5] && dataatrec[3]==data[3] ) {
    printf("\nNo error while transmission of data\n");
    }
    else {
    printf("\nError on position %d",c);

    printf("\nData sent : ");
    for(i=0;i<7;i++)
    printf("%d",data[i]);

    printf("\nData received : ");
    for(i=0;i<7;i++)
    printf("%d",dataatrec[i]);

    printf("\nCorrect message is\n");

    if(dataatrec[7-c]==0)
    dataatrec[7-c]=1;
    else
    dataatrec[7-c]=0;

    for (i=0;i<7;i++) {
    printf("%d",data[i]);
    }
    }
    }

    Reply
    1. HD

      Any exceptions ? Why ur adding if(c==0 && dataatrec[6]==data[6] && dataatrec[5]==data[5] && dataatrec[3]==data[3] )

      Reply
  2. abid ghori

    i need Left factoring Elimination program urgently..please do asap!!

    Reply
  3. poonam

    I need a program in C for FIR filter.Showing its impulse response and magnitude response

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *