剑指offer编程题——10 二进制中1的个数

#include <iostream>
using namespace std;
/*
	输入一个数N,求它的二进制中的1的个数
	①:每次左移N,做N & 1运算;
	②:每次右移1,做N & new1运算;
	③:每次做 N= N & (N-1)运算,会把最右边的1变成0
*/

int NumberOf1_1(int n){ //不可取
	int cnt = 0;
	while (n){
		if (n & 1)
			++cnt;
		n = n >> 1;
	}
	return cnt;
}

int NumberOf1_2(int n){
	int cnt = 0;
	int oper = 1;
	while (oper){
		if (n & oper)
			++cnt;
		oper = oper << 1;
	}
	return cnt;
}

int NumberOf1_3(int n){
	int cnt = 0;
	while (n){
		n = n & (n - 1);
		++cnt;
	}
	return cnt;
}

void main10(){
	int number = -32;
//	int cnt1 = NumberOf1_1(number); //负数会陷入无限循环中
//	cout << "1的个数:" << cnt1 << endl;

	int cnt2 = NumberOf1_2(number);
	cout << "1的个数:" << cnt2 << endl;

	int cnt3 = NumberOf1_2(number);
	cout << "1的个数:" << cnt3 << endl;

	cout << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值