class Solution(object):
def hammingWeight(self, n):
res = 0
while n > 0:
res += n % 2
n /= 2
return res
Python. O(logn) O(1) space

@pisskidney n/=2 gives you a floating point result for odd numbers
try importing math.floor and using it here :)