#include <iostream>using namespace std;int func(int x){ int count=0; while(x) { x=x&(x-1);//与的次数就是包含1的个数 count++; } return count;}
void main(){ int x; cin>>x; cout<<func(x)<<endl; }
将一个数转化为二进制后,求其中1的个数 C++
原文:http://www.cnblogs.com/alanhu/p/4455944.html