Skip to content

Instantly share code, notes, and snippets.

@ndubey
Created January 1, 2017 11:12
Show Gist options
  • Save ndubey/8361c146a7ef02a3ca37de85aea0d344 to your computer and use it in GitHub Desktop.
Save ndubey/8361c146a7ef02a3ca37de85aea0d344 to your computer and use it in GitHub Desktop.
# count number of bits set in binary representation doesn't work on negative numbers
print("Enter a positive integer in decimal form:")
num = int(input())
cnt = 0
while num != 0:
cnt += num&1
num = num>>1
print(num)
print("Number of bits set: %d" %cnt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment