计算十进制数转化成二进制时1的个数

 
 
#include <iostream>

using namespace std;

int func(int x)
{
    int cnt = 0;
    while (x)
    {
        cnt++;
        x = x&(x - 1);
    }
    return cnt;
}

int main()
{
    cout << func(9999) << endl << func(8);
    cin.get();
    return 0;
}


猜你喜欢

转载自blog.csdn.net/jxgt5216/article/details/80296368