191. 位1的个数

class Solution {
    public int hammingWeight(int n) {
        int counter = 0;
        int flag = 1;

        while (flag != 0) {
            if ((n & flag) != 0) {
                counter++;
            }

            flag <<= 1;
        }

        return counter;
    }
}
image.png

你可能感兴趣的:(191. 位1的个数)