public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        while(n != 0){
            if((n&1)!=0){
                count++;
            }
            n = n>>>1;
        }
        return count;

    }
}

//进行计算的次数只与1的个数有关。每次消掉最右边的1,只有数字为0时(即一个1都没有的时候,n才会变成0),才会跳出循环,即count为所求

public class Solution{
    public int NumberOf1(int n){
        int count = 0;
        while(n != 0){
            n = n & (n - 1);
            count++;
        }
        return count;
    }
}