《程序员代码面试指南》 整数的二进制表达式中有多少1

题目:

给定一个32位整数n,可为0,可为正,也可以为负,返回该整数二进制表达式中1的个数。

解答:

最简单解法。整数n每次进行无符号右移一位,检查最右边的bit是否为1来进行统计。


 

public static int count1(int n){

    int res = 0;

    while (n != 0){

        res += n & 1;

    }

    return res;

}

第二种方法每次抹去最右边的1。

//第二种方法每次抹去最右边的1。

public static int count2(int n){

    int res = 0;

    while (n != 0){

        n &= (n-1);

        res++;

    }

    return res;

}

参考资料:《程序员面试代码指南》左程云 著

猜你喜欢

转载自blog.csdn.net/young_1004/article/details/83211887
今日推荐