leetcode - Number of 1 Bits

https://leetcode.com/problems/number-of-1-bits/

bit manipulation

class Solution(object):
    def hammingWeight(self, n):
        """ :type n: int :rtype: int """
        res = 0
        while n:
            res += n & 1
            n >>= 1
        return res

你可能感兴趣的:(LeetCode)