c++ bitset 用法

来源:互联网 发布:软件设计方案评审 编辑:程序博客网 时间:2024/05/17 22:49
#include<iostream>#include<cstdio>#include<bitset>using namespace std;int main(){    int n;    freopen("in.txt","r",stdin);    while(scanf("%d",&n)!=EOF)    {        bitset<32> b(n);//用n进行初始化,位数是32位。n超过位数时,取低位值。        cout<<b;//直接输出时,左边是高位,右边是低位。只能用cout输出。        printf("\n");        cout<<"1的个数:"<<b.count()<<endl;        b.flip();        cout<<"取反:";//能引起变化。        for(int i=0; i<32; i++)            cout<<b[i];//从低位到高位存储,从低位到高位输出。        cout<<endl;        cout<<"1的个数:"<<b.count()<<endl;        b.reset();//重置为0.        cout<<"重置:"<<b<<endl;    }    return 0;}


0 0