位运算

1.

欲求, 如:

00111

依次前移 01011

01101

01110

10011

10101

10110

11001

11010

11100

例如 当k=00111时 获得01011

先找出k右起第一次出现1的位置,对这个位置+1,然后在右边

补上少了的1即可

如 00111+1 = 01000 补 01011

实现

用 p=(num & -num) 即可找到 右起第一次出现1的位置

#include<cstdio>
#include<algorithm>
using namespace std;
int getans(int cmp,int n){
   int frist=(cmp & -cmp);
   int t=cmp+frist;
   int next=(((t^cmp)>>2)/frist)|t;
   if((1<<n)<next) return 0;
   return next;
}
int main(){
    freopen("combination.in","r",stdin);
    freopen("combination.out","w",stdout);
   int n,m;
   scanf("%d%d",&n,&m);
   if(m==0) {
        printf(" ");
    return 0;
   }
int   cmp=(1<<m)-1;
   do{
   for (int i=0;i<n;i++)
       if((cmp&(1<<i))>0) printf("%d",i+1);
       printf(" ");
   }while(cmp=getans(cmp,n));
return 0;
}
2.

当数 a >0时

将其按位取反~a 得到 -1-a.

#include<cstdio>
#include<algorithm>
using namespace std;

int main(){
    int n;
    scanf("%d",&n);
    printf("%d
",~n);
    printf("%d
",-1-n);
return 0;
}

3,0x3f3f3f3f是int能表示的最大值

memset(a,0x3f,sizeof(a));

4.

右移等于向下取整

如:(-3)>>1=-2

直接除2等于向0取整

如: -3/2=-1

原文地址:https://www.cnblogs.com/lmjer/p/8097691.html