Skip to content

Instantly share code, notes, and snippets.

@embed
Created July 31, 2009 12:14
Show Gist options
  • Save embed/159199 to your computer and use it in GitHub Desktop.
Save embed/159199 to your computer and use it in GitHub Desktop.
unsigned hamdist(unsigned x, unsigned y)
{
unsigned dist = 0, val = x ^ y;
// Count the number of set bits (Knuth's algorithm)
while(val)
{
++dist;
val &= val - 1;
}
return dist;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment