bitset指令一览

· · 算法·理论

#include<bits/stdc++.h>
#define int long long
using namespace std;

bitset<20> a,b;
//等价 
bool e[20],f[20];


signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    a[0] = 1;
    a[1] = 1;
    cout<<a.size()<<endl;//20
    cout<<a.count()<<endl;//计数多少个1 // 2
    a.set(10);//a[10] = 1;
    a.set(1,0);//a[1] = 0;
    cout<<a.count()<<endl; //2
    if(a.any()) cout<<"a里面存在1\n";
    if(a.none()) cout<<"a里面没有1\n";
    if(a.all()) cout<<"a里面全是1\n"; 
    a.flip(0); //翻转第0位,1->0 
    a.flip(0); //           0->1
    cout<<a.to_string()<<endl;//输出全部(从高位->低位(20->0)) 
    a <<= 2; //整体使用 
    cout<<a.to_string()<<endl;//12,2位为1,10+2,0+2
    a = a >> 1;
    cout<<a.to_string()<<endl;//11,1位为1,12-1,2-1
    //bitset 支持位运算,&,|等 
    b[11]=1,b[6]=1;
    bitset<20> c=a&b;
    cout<<c.to_string()<<endl;//11位1,其余0
    bitset<20> d=a|b; 
    cout<<d.to_string()<<endl;//11,6,1位为1,其余为0 

    //左移,右移,遍历,位运算复杂度为 O(N/32) 

    cout<<d.to_ulong()<<endl;//转无符号整数 //2114
    cout<<d.to_ullong()<<endl;//转无符号long long //2114
    return 0;
}