PS: 不将整数转化为二进制形态,求该整数的二进制形式中1的个数
#include <iostream>
class Count_1_num {
public:
int count(int n) {
int res = 0;
while (n) {
n = n & (n - 1);
res++;
}
return res;
}
};
PS: 不将整数转化为二进制形态,求该整数的二进制形式中1的个数
#include <iostream>
class Count_1_num {
public:
int count(int n) {
int res = 0;
while (n) {
n = n & (n - 1);
res++;
}
return res;
}
};