位运算用法

1.计算一个数种的二进制数一共有多少个1

思路1,每次判断x&1是否等于1如果是ans+1,x向右移一位(x>>1)当x<=0时退出循环

思路2,用x&-x找出最右边的1并且用让x=x-(x&x-1)更新x,ans+1,当x<=0时退出循环

在某种程度上思路2比思路1更快。比如1000000000000....(1后面一个有1000个0),在思路1里运行101次,思路2运行1次;但是在1111111111....(一共有100个1)里,思路1和思路2都是运行100次。

思路1:

#include<bits/stdc++.h>
#define ll long long
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
#define hh 0x3f3f3f3f
using namespace std;
inline int read()
{
	ll s=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){s=(s<<3)+(s<<1)+c-'0';c=getchar();}
	return s*f;
}
void solve()
{
	ll ans=0,n;
	cin>>n;
	while(n)
	{
		if(n&1)ans++;
		n=>>1;
	}
	cout<<ans<<endl;
}
int main()
{
	ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}

思路2:

#include<bits/stdc++.h>
#define ll long long
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
#define hh 0x3f3f3f3f
using namespace std;
inline int read()
{
	ll s=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){s=(s<<3)+(s<<1)+c-'0';c=getchar();}
	return s*f;
}
void solve()
{
	ll ans=0,n;
	cin>>n;
	while(n)
	{
		n-=(n&-n);
		ans++;
	}
	cout<<ans<<endl;
}
int main()
{
	ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}

2.判断x=2^n(n=0,1,2,3,4,5);

当n=0,时 x=1,其二进制为1

当n=1,时 x=2,其二进制为10

当n=2,时 x=4,其二进制为100

当n=3,时 x=8,其二进制为1000...

我们会发现如果x=2^n,那么其二进制只会出现一个1,因此,我们就可以通过求出x的二进制中1个个数就可以知道x是否等于2^n

#include<bits/stdc++.h>
#define ll long long
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
#define hh 0x3f3f3f3f
using namespace std;
inline int read()
{
	ll s=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){s=(s<<3)+(s<<1)+c-'0';c=getchar();}
	return s*f;
}
void solve()
{
	ll n,cnt=0;
	cin>>n;
	while(n)
	{
		n-=(n&-n);
		cnt++;
	}
	if(cnt==1)
    {
		printf("YES\n");
	}
	else printf("NO\n");
}
int main()
{
	ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}

3.给你一个数组num,全是两个两个成对的数,只有一个落单了,求那个落单的数。比如:2 3 4 5 4 3 2,输出:5;99 55 55 99 44 44 6 6 789,输出:789。

思路:我们要知道a^a=0,0^b=b,a^a^b=b的性质,因此我们在一个循环里,每次分别对f[i]进行^运算,那么我们最后就得出只出现一次的b

#include<bits/stdc++.h>
#define ll long long
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
#define hh 0x3f3f3f3f
using namespace std;
inline int read()
{
	ll s=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){s=(s<<3)+(s<<1)+c-'0';c=getchar();}
	return s*f;
}
void solve()
{
	int n,ans=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		ans=ans^x;
	}
	cout<<ans<<endl;
}
int main()
{
	ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值