Created
October 4, 2022 04:30
-
-
Save m4hi2/c34218e804a7e21cf4236e0f39e63926 to your computer and use it in GitHub Desktop.
bit counter example from EPI book
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def count_bits(x): | |
num_bits = 0 | |
while x: | |
num_bits += x & 1 | |
x = x >> 1 | |
return num_bits | |
for i in range(10): | |
print(count_bits(i)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment