-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamming.py
44 lines (30 loc) · 1.05 KB
/
hamming.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Hamming Weight and Hamming Distance functions for integer values."""
def main():
"""Demo usage of functions."""
print("Hamming Weight of integer {0} is: {1}.".format(9741, hw(9741)))
print("Hamming Distance of integers {0} and {1} is: {2}.".format(106, 219, hd(106, 219)))
def hw(a):
"""Calculate the Hamming Weight of an input.
Keyword arguments:
a -- the input as an integer
"""
if not isinstance(a, int):
raise TypeError("Input parameter must be an integer.")
count = 0
while a:
# Source: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
a &= a - 1
count += 1
return count
def hd(a, b):
"""Calculate the Hamming Distance of two inputs.
Keyword arguments:
a -- the first input as an integer
b -- the second input as an integer
"""
if not isinstance(a, int) or not isinstance(b, int):
raise TypeError("Input parameters must be an integer.")
distance = hw(a ^ b)
return distance
if __name__ == "__main__":
main()