-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhamming_ops.py
More file actions
37 lines (29 loc) · 833 Bytes
/
hamming_ops.py
File metadata and controls
37 lines (29 loc) · 833 Bytes
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
def hamming_distance(x: int, y: int):
"""
Calculate the number of bits that are different between 2 numbers.
Args:
x: first integer to calculate distance on
Y: second integer to calculate distance on
Returns:
An integer representing the number of bits that are different
in the two args.
"""
# xor numbers to get diff bits as 1's
xor = x ^ y
# count the 1's in integer
return hamming_weight(xor)
def hamming_weight(x: int):
"""
Count the number of on bits in an integer.
Args:
x: the number to count on bits
Returns:
Integer representing the number of bits
that are on (1).
"""
count = 0
while x:
# bitwise AND number with itself minus 1
x &= x - 1
count += 1
return count