位运算的“奇巧淫技”

 

#include<bits/stdc++.h>
using namespace std;

int getbits(int n)//统计(n)bin中的1的个数 
{
	int res=0;
	while(n)
	{
		res++;
		n=n&(n-1);
	}
	return res;
}

int cmp(int a,int b)//统计a,b中the number of different bits in the binary format of a and b. 
{
	return  getbits(a^b);	
} 

int main()
{
	int a,b;
	while(cin>>a>>b) cout<<cmp(a,b)<<endl;
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/dragondragon/p/11340440.html
今日推荐