输入一个int型正整数,输出它的二进制形式中数字1的个数


如输入: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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM