C++ - 求一个正整数的二进制表示中1的个数

版权声明:欢迎转载并请注明出处,谢谢~~ https://blog.csdn.net/chimomo/article/details/7716641
/*
 * Created by Chimomo
 */

#include <iostream>

#define NULL 0

using namespace std;

int f(int x) {
    int n = 0;
    while (x) {
        n++;
        x &= x - 1;
    }
    return n;
}

int main() {
    cout << f(26) << endl;
    return 0;
}

// Output:
/*
3

*/

猜你喜欢

转载自blog.csdn.net/chimomo/article/details/7716641