191。Number of1 Bits

来源:互联网 发布:天龙八部辅助软件 编辑:程序博客网 时间:2024/05/28 23:09
/*
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.
*/

#define max 1010
int hammingWeight(uint32_t n) {
    uint32_t i,count = 0,temp = n;
    int a[max] = {0};
    for(i = 0 ;  n!= 0 ; i++)
    {
        a[i] = n % 2;
        n = n / 2;
    }
    for(i=0;temp != 0;i++)
    {
        temp = temp / 2;
        if(a[i]==1)
            count++;
    }
    return count;
}

原创粉丝点击