基本知识:
1. 与 x&y
或 x|y
非 !x
异或 x^y
2. 移位
1<<=n = 2^n//将1的二进制形式向右移n位
n>>=x = n/2^x//将n的二进制形式向左移x位
3. 补码
首先计算机规定: 1+1111......11111=0000....00000(32位)
2+1111......11110=0000....00000(32位)
x+ ? =0000....00000(32位)
...
我们规定?为x的补码,所以x的相反数?=~x+1(即 ~x+1是x的补码,在计算机中(int)输出结果为x的相反数)
4. lowbit运算
例: lowbit(1110010000)=10000
int lowbit(int n)
{
return n&(-n);
}
4.lowbit
题目描述:统计一个数列里有多少2次幂数
测试样例:
输入:
4
1 2 4 8
输出:
4
代码如下:
#include<iostream>
using namespace std;
long long f(long long n);
int main()
{
int n,count=0;
long long a;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lld",&a);
if(f(a)==a)
count++;
}
printf("%lld",count);
return 0;
}
long long f(long long n)
{
return (-n)&n;
}
5.异或前缀和