Advertisement
Shishu

Hamming Code in C

Nov 24th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.90 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. void main()
  4. {
  5.     int data[10];
  6.     int dataatrec[10],c,c1,c2,c3,i;
  7.  
  8.     printf("Enter 4 bits of data one by one:\n");
  9.     scanf("%d",&data[0]);
  10.     scanf("%d",&data[1]);
  11.     scanf("%d",&data[2]);
  12.     scanf("%d",&data[4]);
  13.  
  14.     //Calculation of even parity
  15.     data[6]=data[0]^data[2]^data[4];
  16.     data[5]=data[0]^data[1]^data[4];
  17.     data[3]=data[0]^data[1]^data[2];
  18.  
  19.     printf("\nEncoded data is\n");
  20.     for(i=0; i<7; i++)
  21.         printf("%d",data[i]);
  22.  
  23.     printf("\n\nEnter received data bits one by one\n");
  24.     for(i=0; i<7; i++)
  25.         scanf("%d",&dataatrec[i]);
  26.  
  27.     c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0];
  28.     c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];
  29.     c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];
  30.     c=c3*4+c2*2+c1 ;
  31.  
  32.     if(c==0)
  33.     {
  34.         printf("\nNo error while transmission of data\n");
  35.     }
  36.     else
  37.     {
  38.         printf("\nError on position %d",c);
  39.  
  40.         printf("\nData sent : ");
  41.         for(i=0; i<7; i++)
  42.             printf("%d",data[i]);
  43.  
  44.         printf("\nData received : ");
  45.         for(i=0; i<7; i++)
  46.             printf("%d",dataatrec[i]);
  47.  
  48.         printf("\nCorrect message is\n");
  49.  
  50.         //if errorness bit is 0 we complement it else vice versa
  51.         if(dataatrec[7-c]==0)
  52.             dataatrec[7-c]=1;
  53.         else
  54.             dataatrec[7-c]=0;
  55.  
  56.         for (i=0; i<7; i++)
  57.         {
  58.             printf("%d",dataatrec[i]);
  59.         }
  60.     }
  61. }
  62.  
  63.  
  64. another hamming code...
  65.  
  66. #include <stdio.h>
  67. #include <math.h>
  68. int input[32];
  69. int code[32];
  70. int ham_calc(int,int);
  71. void main()
  72. {
  73.     int n,i,p_n = 0,c_l,j,k;
  74.     printf("Please enter the length of the Data Word: ");
  75.     scanf("%d",&n);
  76.     printf("Please enter the Data Word:\n");
  77.     for(i=0;i<n;i++)
  78.     {
  79.         scanf("%d",&input[i]);
  80.     }
  81.  
  82.     i=0;
  83.     while(n>(int)pow(2,i)-(i+1))
  84.     {
  85.         p_n++;
  86.         i++;
  87.     }
  88.        
  89.     c_l = p_n + n;
  90.  
  91.     j=k=0;
  92.     for(i=0;i<c_l;i++)
  93.     {
  94.        
  95.         if(i==((int)pow(2,k)-1))
  96.         {
  97.             code[i]=0;
  98.             k++;
  99.         }
  100.         else
  101.         {
  102.             code[i]=input[j];
  103.             j++;
  104.         }
  105.     }
  106.     for(i=0;i<p_n;i++)
  107.     {
  108.         int position = (int)pow(2,i);
  109.         int value = ham_calc(position,c_l);
  110.         code[position-1]=value;
  111.     }
  112.     printf("\nThe calculated Code Word is: ");
  113.     for(i=0;i<c_l;i++)
  114.         printf("%d",code[i]);
  115.     printf("\n");
  116.     printf("Please enter the received Code Word:\n");
  117.     for(i=0;i<c_l;i++)
  118.         scanf("%d",&code[i]);
  119.  
  120.     int error_pos = 0;
  121.     for(i=0;i<p_n;i++)
  122.     {
  123.         int position = (int)pow(2,i);
  124.         int value = ham_calc(position,c_l);
  125.         if(value != 0)
  126.             error_pos+=position;
  127.     }
  128.     if(error_pos == 1)
  129.         printf("The received Code Word is correct.\n");
  130.     else
  131.         printf("Error at bit position: %d\n",error_pos);
  132. }
  133. int ham_calc(int position,int c_l)
  134. {
  135.     int count=0,i,j;
  136.     i=position-1;
  137.     while(i<c_l)
  138.     {
  139.         for(j=i;j<i+position;j++)
  140.         {
  141.             if(code[j] == 1)
  142.                 count++;
  143.         }
  144.         i=i+2*position;
  145.     }
  146.     if(count%2 == 0)
  147.         return 0;
  148.     else
  149.         return 1;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement