判断32位无符号整数二进制形式有多少个1.
// 191.Number of 1 Bits
int solution::hammingWeight(unsigned int n)
{
int a = 1;
int count = 0;
for (int i = 1; i <= 32; ++i)
{
if ((a & n) == 1)
{
++count;
}
n = n >> 1;
}
return count;
}
判断32位无符号整数二进制形式有多少个1.
// 191.Number of 1 Bits
int solution::hammingWeight(unsigned int n)
{
int a = 1;
int count = 0;
for (int i = 1; i <= 32; ++i)
{
if ((a & n) == 1)
{
++count;
}
n = n >> 1;
}
return count;
}