##位运算若干应用##

//把右起第一个0变成1    | (100101111->100111111)    | x or (x+1)
//把右边连续的0变成1    | (11011000->11011111)      | x or (x-1)
//取右边连续的1         | (100101111->1111)         | (x xor (x+1)) shr 1
//取右起的第一个1       | (100101000->1000)         | x and (-x)
#include <cstdio>
#include <cstring>
#include <algorithm>
/*A:x &= x-1;*/
//usage1:
    bool is_pow2(int x) //判断x是否2的n次方,x>0,x=1,2,4,……8 return 1;
    {
        x&=x-1;return (x==0);
    }
//usage2:
    int count1(int x)//计算数number的二进制中包含1的个数,x>0
    {
        int co=0;
        while(x)
        {
            co++;
            x&=x-1;
        }
	return co;
    }
//usage3:
	void enumerate_subset(int x) //x>=0
	{
	    int y=x;
	    do{
           //printf("%d\n",y);
           y=(y-1)&x;
        }while (y!=x); //处理完0后,会有-1&x=x;
	}
/* */

/*B:枚举x的所有大小为k的子集*/
void enumerate_subset_ofsize_k(int k,int n)
{
    int cc=(1<<k)-1,tot=1<<n;
    while (cc<tot)
        {   //printf("%d\n",cc);
            int x=cc&(-cc),y=cc+x;
            cc=((cc&(~y))/x>>1)|y;
        }
}
/*C:枚举不含相邻元素的集合*/
void enumerate_subset_of_noconsecutive(int n)
{
    int tot=1<<n,t;
    for (int i=0;;)
        {   //printf("%d\n",i);
            t=i|(i>>1);
            t=tot-1-t;
            t=t&(-t);
            if (t==0)break;
            i=(i+t)&(tot-t);
        }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值