位运算的“奇巧淫技”

不问归期 提交于 2019-11-27 04:59:51

 

 

 

 

 

 

 

 

 

 

#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;
}

  

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!