整数的二进制表达中有多少个1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wzc2608/article/details/81178036
#include <iostream>
using namespace std;
int count1(int n)
{
    int res = 0;
    while(n != 0)
    {
        res += n & 1;
        n >>= 1;
    }
    return res;
}
int count2(int n)
{
    int res = 0;
    while(n != 0)
    {
        n &= (n - 1);
        ++res;
    }
    return res;
}
int count3(int n)
{
    int res = 0;
    while(n != 0)
    {
        n -= n & (~n + 1);
        ++res;
    }
    return res;
}
int count4(int n)
{
    n = (n & 0x55555555) + ((n >> 1) & 0x55555555);
    n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
    n = (n & 0x0f0f0f0f) + ((n >> 4) & 0x0f0f0f0f);
    n = (n & 0x00ff00ff) + ((n >> 8) & 0x00ff00ff);
    n = (n & 0x0000ffff) + ((n >> 16) & 0x0000ffff);
    return n;
}
int main()
{
    int n;
    cin >> n;
    cout << count1(n) << endl;
    cout << count2(n) << endl;
    cout << count3(n) << endl;
    cout << count4(n) << endl;
}

猜你喜欢

转载自blog.csdn.net/wzc2608/article/details/81178036