8000 Minor changes. · 42Tolikdev/practice-python@91cea47 · GitHub
[go: up one dir, main page]

Skip to content

Commit 91cea47

Browse files
committed
Minor changes.
1 parent 078f49b commit 91cea47

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

bit_manipulation/bits.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
21
def hamming_distance(x, y):
32
difference = x ^ y
43
count = 0
5-
while difference != 0:
6-
count += 1
4+
while difference:
75
# this removes ones from right to left (least to most significant)
86
difference &= difference - 1
7+
count += 1
98
return count
109

1110

12-
# Kernighan method
11+
# Wegner method
1312
def hamming_weight(x):
13+
if x < 0:
14+
return None
15+
1416
count = 0
15-
while x != 0:
16-
count += 1
17+
while x:
1718
x &= x - 1
19+
count += 1
20+
1821
return count
1922

2023

2124
def pop_count(i):
22-
i = i - ((i >> 1) & 0x55555555)
25+
i -= ((i >> 1) & 0x55555555)
2326
i = (i & 0x33333333) + ((i >> 2) & 0x33333333)
2427
return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24
2528

@@ -205,5 +208,6 @@ def main():
205208
print("swap 213, 14", swap_ints(213, 14))
206209
print("swap 872, 992", swap_ints(872, 992))
207210

211+
208212
if __name__ == "__main__":
209213
main()

0 commit comments

Comments
 (0)
0