第9讲6*
//法1
class Solution {
public:
int NumberOf1(int n) {
int res = 0;
for (int i = 0; i < 32; i++)
{
if (n >> i & 1) res++;
}
return res;
}
};
法2:lowbit
class Solution {
public:
int NumberOf1(int n) {
int res = 0;
while (n) n -= n & -n, res++;
return res;
}
};