Number of 1 Bits
题意:计算一个无符号整形数字的二进制格式下1的个数
思路:在while循环中首先用这个数求余2,如果余数为1,则个数加1,然后 将该数字除以2,判断该数字是否为0,是结束循环,否则继续
class Solution:
# @param n, an integer
# @return an integer
def hammingWeight(self, n):
res = 0
while n != 0:
if n%2 == 1:
res = res+1
n = n/2
return res