美文网首页
统计非负整数二进制展开中数位1的总数

统计非负整数二进制展开中数位1的总数

作者: 鹿与云与雨 | 来源:发表于2019-11-27 15:45 被阅读0次

统计非负整数二进制展开中数位1的总数。如整数64 的二进制展开为00000000 00000000 00000000 00100000 ,数位1的总数为1。

#include<iostream>
using namespace std;

long long int caculate(long long int n)
{
    long long int result = 0;
    while (0 < n)
    {
        result++;
        n = n & (n - 1);
    }
    return result;
}

int main()
{
    long long int o;
    cin >> o;
    cout << caculate(o);
    return 0;
}

相关文章

网友评论

      本文标题:统计非负整数二进制展开中数位1的总数

      本文链接:https://www.haomeiwen.com/subject/ttwpwctx.html