常规解法:
#include <iostream>
using namespace std;
class Solution {
public:
int byteCountOne(int n)
{
int count = 0;
//unsigned int flag = 1;
while (n)
{
if (n & 1)
count++;
n = n >> 1;
}
return count;
}
};
int main()
{
Solution s;
cout << s.byteCountOne(11);
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class Solution {
public:
int byteCountOne(int n)
{
int count = 0;
unsigned int flag = 1;
while (flag)
{
if (n & flag)
count++;
flag = flag << 1;
}
return count;
}
};
int main()
{
Solution s;
cout << s.byteCountOne(11);
system("pause");
return 0;
}