状压dp常用技巧—Bitset

状压dp通常由于空间限制需要用到二进制。
这里介绍一种二进制处理方法——biset。

#include<cstdio>
#include<iostream>
#include<bitset>
#include<cstring>
using namespace std;
bitset<128> num(10),num1(-10);
int main(){ 
    cout<<num.to_ulong()<<endl;
    for(int i=3;i>=0;i--){
        cout<<num[i];
    }
    cout<<endl;

    cout<<num<<endl;
    cout<<num.flip()<<endl;
    cout<<num1<<endl;


    bitset<4> foo (string("1001"));
    bitset<4> bar (string("0011"));


    cout<<"第一个数"<<foo<<endl;
    cout<<(foo[1]|1)<<endl;
    cout<<"第二个数"<<bar<<endl;

    cout << (foo^=bar) << '\n';       // 1010 (XOR,assign)
    cout << (foo&=bar) << '\n';       // 0010 (AND,assign)
    cout << (foo|=bar) << '\n';       // 0011 (OR,assign)

    cout << (foo<<=2) << '\n';        // 1100 (SHL,assign)
    cout << (foo>>=1) << '\n';        // 0110 (SHR,assign)

    cout << (~bar) << '\n';           // 1100 (NOT)
    cout << (bar<<1) << '\n';         // 0110 (SHL)
    cout << (bar>>1) << '\n';         // 0001 (SHR)

    cout << (foo==bar) << '\n';       // false (0110==0011)
    cout << (foo!=bar) << '\n';       // true  (0110!=0011)

    cout << (foo&bar) << '\n';        // 0010
    cout << (foo|bar) << '\n';        // 0111
    cout << (foo^bar) << '\n';        // 0101

    foo.size() ; //返回大小(位数)
    foo.count() ; //返回1的个数
    foo.any() ; //返回是否有1
    foo.none() ; //返回是否没有1
    cout<<foo<<endl;
    foo.set() ; //全都变成1
    int p=1,x=1;
    cout<<foo<<endl;
    foo.set(p) ; //将第p + 1位变成1

    foo.flip() ; //全都取反
    cout<<"前:"<<foo<<endl;
    foo.set(p, x) ; //将第p + 1位变成x
    cout<<"后:"<<foo<<endl;
    foo.reset() ; //全都变成0
    foo.reset(p) ; //将第p + 1位变成0

    foo.flip(p) ; //将第p + 1位取反

    cout<<foo.to_ulong()<<endl; //返回它转换为unsigned long的结果,如果超出范围则报错
//  cout<<foo.to_ullong()<<endl; //返回它转换为unsigned long long的结果,如果超出范围则报错
    foo.to_string() ; //返回它转换为string的结果

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值