直接用左移操作符和按位与操作符解决:
代码块如下所示:
int hammingWeight(int n)
{
int count = 0;
while(n != 0)
{
if((n & 1) == 1)
{
count++;
}
n = n>>1;
}
return count;
}
直接用左移操作符和按位与操作符解决:
代码块如下所示:
int hammingWeight(int n)
{
int count = 0;
while(n != 0)
{
if((n & 1) == 1)
{
count++;
}
n = n>>1;
}
return count;
}