Skip to content

Instantly share code, notes, and snippets.

@m4hi2
Created October 4, 2022 04:30
Show Gist options
  • Save m4hi2/c34218e804a7e21cf4236e0f39e63926 to your computer and use it in GitHub Desktop.
Save m4hi2/c34218e804a7e21cf4236e0f39e63926 to your computer and use it in GitHub Desktop.
bit counter example from EPI book
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