如输入:3=====》》》二进制101 所以 输出 =======》》》2
#include<iostream> #include<string> using namespace std; void count_byte(unsigned int x) { unsigned int tmp = 1; int countx = 0; for (int i = 0; i<32; i++) { if ((tmp&x) == 1) { countx++; } x = x >> 1; } cout << countx << endl; } int main() { unsigned int num; cin >> num; count_byte(num); return 0; }
9======》》》二进制00001001 1的个数=======》》》2