Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- void main()
- {
- int data[10];
- int dataatrec[10],c,c1,c2,c3,i;
- 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]);
- //Calculation of even parity
- 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)
- {
- 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 errorness bit is 0 we complement it else vice versa
- if(dataatrec[7-c]==0)
- dataatrec[7-c]=1;
- else
- dataatrec[7-c]=0;
- for (i=0; i<7; i++)
- {
- printf("%d",dataatrec[i]);
- }
- }
- }
- another hamming code...
- #include <stdio.h>
- #include <math.h>
- int input[32];
- int code[32];
- int ham_calc(int,int);
- void 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]);
- }
- 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;
- }
- 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)
- {
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement