***x & (x - 1)***就是将二进制x的最后一个1置为0,如2015的二进制为11111011111,则进行一次 x & (x - 1) 运算后x的值变为11111011110
public static int func(int n){
int count = 0;
while(n!=0){
count++;
n = n & (n-1);
}
return count;
***x & (x - 1)***就是将二进制x的最后一个1置为0,如2015的二进制为11111011111,则进行一次 x & (x - 1) 运算后x的值变为11111011110
public static int func(int n){
int count = 0;
while(n!=0){
count++;
n = n & (n-1);
}
return count;