Skip to content

Instantly share code, notes, and snippets.

@gregbugaj
Last active September 20, 2017 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregbugaj/812d6414b5c22f1c281f06f66b5b4a9e to your computer and use it in GitHub Desktop.
Save gregbugaj/812d6414b5c22f1c281f06f66b5b4a9e to your computer and use it in GitHub Desktop.
Hamming distance calculation
typedef unsigned long int hash_t;
#include <iostream>
#include <bitset>
#include <iostream>
#include <bitset>
#include <climits>
#include "HashDistance.h"
int popcnt(const hash_t& val) noexcept
{
int ret;
__asm__ ("popcnt %1, %1" : "=r" (ret) : "0" (val));
return ret;
}
constexpr size_t hashsize()
{
return CHAR_BIT * sizeof(hash_t);
}
int HashDistance::distance(const hash_t &x, const hash_t &y) const
{
auto z = x ^ y;
auto p = popcnt(z);
if(debug)
{
std::cout<<"size : " << std::dec << hashsize() << std::endl;
std::cout<<"x val : " << std::bitset<hashsize()>(x) << std::endl;
std::cout<<"y val : " << std::bitset<hashsize()>(y) << std::endl;
std::cout<<"z val : " << std::bitset<hashsize()>(z) << std::endl;
std::cout<<"pop : " << p << std::endl;
}
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment