程式語言 - LeetCode - C++ - 191. Number of 1 Bits



題目:


解答:

class Solution {
public:
    int hammingWeight(int n) {
        int ans = 0;

        for (int i = 0; i < 32; ++i) {
            ans += (n & 1);
            n >>= 1;
        }

        return ans;
    }
};