易百教程

45、编写一个程序来计算整数中的集合位?

请参考下面代码实现:

unsigned int NumberSetBits(unsigned int n)
{
    unsigned int CountSetBits= 0;
    while (n)
    {
        CountSetBits += n & 1;
        n >>= 1;
    }
    return CountSetBits;
}