剑指offer之二进制中1的个数

来源:互联网 发布:jsp商城系统源码 编辑:程序博客网 时间:2024/05/20 00:39

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

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

n&(n-1)每次消除最右边一个1


原创粉丝点击