求一个整数储存在内存中的二进制中1的个数(可求负数)

#include <stdio.h>
#include <stdlib.h>
int main(){
	int num = -10;
	int count = 0;
	for (int i = 0; i < 32; ++i){
		if (num & (1 << i)){	//依次比较二进制位置看是否为1
			++count;
		}
	}
	printf("%d\n",count);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44781107/article/details/89296165
今日推荐