C program to Calculate Hamming Distance of two Binary Strings

In the field of Information theory, Hamming distance of two equal length strings is the number of position at which the corresponding symbols are different or we can say that it is the minimum number of substitution required to change one string into other.

In case of two binary strings we can calculate hamming distance by doing XOR operation on the two strings and the number of 1 present in the result will be the Hamming Distance, for example

1 0 0 1 0
1 1 0 0 0
—————
0 1 0 1 0 (Hamming distance is two as number of 1 in the result is two)

Now we will write a C program to calculate the Hamming distance of two binary strings.

We are going to make three functions for our C program:

  • input() : This function will be used to take two binary strings in form of an array.
  • hamming(): this function will perform the XOR operation on the two inputs
  • count_ham(): This function will count the number of 1 in the result and will return the output.

Let’s C the code for each function

input()

int input(int ar1[]){ int i; for(i=0;i<n;i++){ scanf("\%d",&ar1[i]); }  }

hamming()

int hamming(int ar1[],int ar2[]){ int i,count; int res[BITS]; for(i=0;i<n;i++){ if((ar1[i]==1 && ar2[i]==0)||(ar1[i]==0 && ar2[i]==1)){ res[i] = 1; } else{ res[i] = 0; } } count = count_ham(res); printf("Hamming distance will be: \%d",count); printf(""); }

count_ham()

int count_ham(int ar[]){ int i,count=0; for(i=0;i<n;i++){ if(ar[i]==1) count++; } return count;  }

Now let’s C the full C program to Calculate Hamming Code of two binary strings

#include<stdio.h> #define BITS 8 int hamming(int ar1[],int ar2[]); int input(int ar1[]); int count_ham(int ar[]); int n; int main(){ int ar1[BITS],ar2[BITS]; printf("Enter the number of bits(max 8-bits):"); scanf("\%d",&n); printf("Enter a binary number(space between each bit and MAX 8-bit):"); input(ar1); printf("Enter a binary number(space between each bit and MAX 8-bit):"); input(ar2); hamming(ar1,ar2); return 0; }  int input(int ar1[]){ int i; for(i=0;i<n;i++){ scanf("\%d",&ar1[i]); }  } int count_ham(int ar[]){ int i,count=0; for(i=0;i<n;i++){ if(ar[i]==1) count++; } return count;  } int hamming(int ar1[],int ar2[]){ int i,count; int res[BITS]; for(i=0;i<n;i++){ if((ar1[i]==1 && ar2[i]==0)||(ar1[i]==0 && ar2[i]==1)){ res[i] = 1; } else{ res[i] = 0; } } count = count_ham(res); printf("Hamming distance will be: \%d",count); printf(""); } 

Output:

C program for Hamming distance

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don’t forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.


Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Image Sharpening by High Pass Filter using Python and OpenCV
17 August 2018

Explaining Register variables in C with examples
17 August 2018

C program to generate all combinations of N-Bit Binary String
10 July 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий