JZ11 二进制中1的个数

题目描述

输入一个整数,输出该数32位二进制表示中1的个数。其中负数用补码表示。

public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        while(n != 0){
            n = n & (n-1);//可以记住,检测n中1的个数     
            count++;
        }
        return count;
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41620020/article/details/108458103