最近在刷NOIP,给大家分享几个知识点:
1.按位与运算(&):
设定两个正整数x和y,x=0,y=1,则:
x&x=0;
x&y=0;
y&x=0;
y&y=1
例如3&5= xxxxxxyy & xxxxxyxy = xxxxxxxy
2.按位或运算(| )
x|x=0;
x|y=1;
y|x=1;
y|y=0
例如3&5= xxxxxxyy & xxxxxyxy = xxxxxxxy
实例:
#include<bits/stdc++.h>
using namespace std;
int a;
int n[1000];
int f(int x)
{
int ret;
for(;x;x&=x-1)
{
ret++;
}
return ret;
}
int main()
{
int n;
scanp(n);
f(n);
cout<<n;
return 0;
}
注释:第08行中的x&=x-1的意思为:将x与(x-1)进行按位与运算,并求解。
结果可以发在评论区(会进行评分oh),886!