Hamming Code in C and C++
In this example, you will know hamming code and get the example of hamming code in c and c++ programming.
What is Hamming Code
Hamming code is an error-detecting and error-correcting code used in digital networks and storage devices to detect and correct data transmission errors. It is a type of block code that is widely used in telecommunication, data storage, and computing.
Hamming code adds extra bits to the data to detect and correct errors. It works by adding parity bits to the data which can be used to detect and correct errors. Hamming code can detect up to continuously 2-bit errors and can correct single-bit errors.
Advantages of Hamming code
- Hamming codes are capable of detecting and correcting errors in data transmission. This is especially useful in noisy communication channels, where bit errors are common.
- Hamming codes are relatively simple and easy to implement.
- Hamming codes can correct single-bit errors as well as detect double–bit errors.
- Hamming codes are used in many different applications, such as error correction in computer networks and data storage.
Disadvantages of Hamming code
- Hamming codes have a relatively high overhead, as they add extra bits to the data transmitted.
- Hamming codes are limited in the number of errors they can detect and correct.
- Hamming codes are not suitable for burst errors, as they can only detect and correct single–bit errors.
Example of Hamming Code in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | //program of Hamming Code in C #include<stdio.h> #include<math.h> #include<stdlib.h> int main() { int i,j,k,count,err_pos=0,flag=0; char dw[20],cw[20],data[20]; printf("Enter data as binary bit stream (7 bits):\n"); scanf("%s",data); for(i=1,j=0,k=0;i<12;i++) { if(i==(int)pow(2,j)) { dw[i]=' ? '; j++; } else { dw[i]=data[k]; k++; } } for(i=0;i<4;i++) { count=0; for(j=(int)pow(2,i);j<12;j+=(int)pow(2,i)) { for(k=0;k<(int)pow(2,i);k++) { if(dw[j]=='1') count++; j++; } } if(count%2==0) dw[(int)pow(2,i)]='0'; else dw[(int)pow(2,i)]='1'; } printf("\n Encoded data is\n\n"); for(i=1;i<12;i++) printf("%c",dw[i]); printf("\n\n Enter the received Data \n\n"); scanf("%s",cw); for(i=12;i>0;i--) cw[i]=cw[i-1]; for(i=0;i<4;i++) { count=0; for(j=(int)pow(2,i);j<12;j+=(int)pow(2,i)) { for(k=0;k<(int)pow(2,i);k++) { if(cw[j]=='1') count++; j++; } } if(count%2!=0) err_pos=err_pos+(int)pow(2,i); } if(err_pos==0) printf("\n\n No error in received code \n"); else { if(cw[err_pos]==dw[err_pos]) { printf("\n\n There are 2 or more errors in received code....\n\n"); printf("Sorry....!Hamming code cannot correct 2 or more errors.....\n"); flag=1; } else printf("\nThere is an error in bit position %d of received code word\n\n",err_pos); if(flag==0) { cw[err_pos]=(cw[err_pos]=='1') ? '0' : '1'; printf("\n\n Corrected data is \n\n"); for(i=1;i<12;i++) printf("%c",cw[i]); } } printf("\n\n"); } |
Example of Hamming Code in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | #include<iostream.h> #include<math.h> #include<stdlib.h> int main() { int i,j,k,count,err_pos=0,flag=0; char dw[20],cw[20],data[20]; cout<<"Enter data as binary bit stream (7 bits):\n"; cin>>data; for(i=1,j=0,k=0;i<12;i++) { if(i==(int)pow(2,j)) { dw[i]=' ? '; j++; } else { dw[i]=data[k]; k++; } } for(i=0;i<4;i++) { count=0; for(j=(int)pow(2,i);j<12;j+=(int)pow(2,i)) { for(k=0;k<(int)pow(2,i);k++) { if(dw[j]=='1') count++; j++; } } if(count%2==0) dw[(int)pow(2,i)]='0'; else dw[(int)pow(2,i)]='1'; } cout<<"\n Encoded data is\n\n"; for(i=1;i<12;i++) cout<<dw[i]; cout<<"\n\n Enter the received Data\n\n"; cin>>cw; for(i=12;i>0;i--) cw[i]=cw[i-1]; for(i=0;i<4;i++) { count=0; for(j=(int)pow(2,i);j<12;j+=(int)pow(2,i)) { for(k=0;k<(int)pow(2,i);k++) { if(cw[j]=='1') count++; j++; } } if(count%2!=0) err_pos=err_pos+(int)pow(2,i); } if(err_pos==0) cout<<"\n\n There is no error in received code word\n"; else { if(cw[err_pos]==dw[err_pos]) { cout<<"\n\n There are 2 or more errors in received code....\n\n"; cout<<"Sorry....!Hamming code cannot correct 2 or more errors.....\n"; flag=1; } else cout<<"\nThere is an error in bit position %d of received code word\n\n",err_pos; if(flag==0) { cw[err_pos]=(cw[err_pos]=='1') ? '0' : '1'; cout<<"\n\n Corrected data is \n\n"; for(i=1;i<12;i++) cout<<cw[i]; } } cout<<"\n\n"; } |
Output