计算二进制中1的个数

来源:互联网 发布:java socket传输byte 编辑:程序博客网 时间:2024/05/20 20:56
#include <iostream>using namespace std;int count_bin(int n){    int count = 0;    while(n)    {        n = n&(n-1);        count++;    }    return count;}int main(){    int n;    while(cin>>n)    {        cout<<count_bin(n)<<endl;    }    return 0;}
原创粉丝点击