Skip to content

Instantly share code, notes, and snippets.

@jcsalterego
Created July 14, 2009 20:14
Show Gist options
  • Select an option

  • Save jcsalterego/147157 to your computer and use it in GitHub Desktop.

Select an option

Save jcsalterego/147157 to your computer and use it in GitHub Desktop.
def hexhamming(a, b, equal=False):
if not equal:
minl = min(len(a), len(b))
a = a[:minl]
b = b[:minl]
lookup = dict(zip("0123456789abcdef", range(16)))
diffs = 0
for c1, c2 in zip(a, b):
if c1 != c2:
diffmask = lookup[c1] ^ lookup[c2]
while diffmask > 0:
diffs += diffmask & 1
diffmask >>= 1
return diffs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment