Skip to content

Instantly share code, notes, and snippets.

@vjames19
Created May 6, 2013 03:53
Show Gist options
  • Save vjames19/5523274 to your computer and use it in GitHub Desktop.
Save vjames19/5523274 to your computer and use it in GitHub Desktop.
Number of Ones code eval
import sys
import math
def numberOfOnes(n):
mask = 1
bits = n.bit_length()
if n < 0:
bits +=1
count = 0
for i in range(0, bits):
count += n & mask
n = n >> 1
print count
test_cases = open(sys.argv[1], 'r')
for test in test_cases:
if test:
numberOfOnes(int(test.strip()))
test_cases.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment