Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11’ has binary representation 00000000000000000000000000001011, so the function should return 3.
找出一个uint数据当中1的个数,非常easy不分析了
#include <stdlib.h>
#include <stdio.h>
typedef unsigned int uint32_t;
typedef unsigned char uchar8_t;
int hammingWeight(uint32_t n) {
uint32_t i = 0;
uint32_t tem = 1;
uint32_t r = 0;
for(i=0;i<32;i++)
{
if(tem & n)
r += 1;
n = n >> 1;
if(n == 0) break;
}
return r;
}
//解法2
int hammingWeight1(uint32_t n)
{
uint32_t r = 0;
while(0 != n)
{
r++;
n &= (n-1);
}
return r;
}
int main()
{
uint32_t n = 0b00000000000100000000000000001011;
int r = hammingWeight(n);
int r1 = hammingWeight1(n);
printf("hammingWeight n has %d 1bit
",r);
printf("hammingWeight n has %d 1bit
",r1);
}