Last active
September 20, 2017 12:53
-
-
Save gregbugaj/812d6414b5c22f1c281f06f66b5b4a9e to your computer and use it in GitHub Desktop.
Hamming distance calculation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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