Monday, 15 June 2015

The Hamming Distance Between A Pair Of Numbers In C++ Programming Language Solution.

/* The hamming distance between a pair of numbers is the number of binary bits that differ in their binary notation. For example:

25: 00011001

87: 01010111
The hamming distance between these two would be 4, the 2nd, 5th, 6th, 7th bit from the right. Both numbers will be less than 2^20.

 Solution In C++ Language
*/

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
 int a,b,ara[100],arb[100],cnt = 0;;
 
 cin >> a >> b;
 int i = 0;
 
 while(a !=0){
  ara[i++] = a%2;
  a/=2;
 }
 i = 0;
 while(b !=0){
  arb[i++] = b%2;
  b/=2;
 }
 for(int j = 0; j < i; j++){
  if(ara[j] == arb[j]){
   cnt++;
  }
 }
 cout << "Answer is: " << cnt << endl;
 return 0;
}

No comments:

Post a Comment