计算一个数中二进制1的个数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int count_one_bits(unsigned val) {
	int count = 0;
	int i = 0;
	for (; i < 32; ++i) {
		if (((val >> i) & 1) == 1) {
			++count;
		}
	}
	return count;
}

int main() {
	unsigned int n = 0;
	scanf("%u", &n);
	printf("%d\n", count_one_bits(n));
	system("pause");
	return 0;
}

127
7
请按任意键继续. . .

猜你喜欢

转载自blog.csdn.net/qq940051592/article/details/86428212
今日推荐