bitset用法

学习博客:https://www.cnblogs.com/magisk/p/8809922.html

C++的 bitset 在 bitset 头文件中,它是一种类似数组的结构,它的每一个元素只能是0或1,每个元素仅用1bit空间。

下面是具体用法

构造函数

bitset常用构造函数有四种,如下

#include<iostream>
#include<bitset>
#include<cstring>
using namespace std;
int main()
{
    bitset<4> bitset1;//无参构造 长度为4  默认值为0
    bitset<8> bitset2(12);//长度为8 二进制保存  前面用0补充
    string s="100101";
    bitset<10> bitset3(s);//长度为10  前面用0补充

    cout<<bitset1<<endl;
    cout<<bitset2<<endl;
    cout<<bitset3<<endl;
/**
输出结果  可以看出 bitset 是从后往前填值的
0000
00001100
0000100101
*/

    return 0;
}

注意:

用字符串构造时,字符串只能包含 '0' 或 '1' ,否则会抛出异常。

构造时,需在<>中表明bitset 的大小(即size)。

在进行有参构造时,若参数的二进制表示比bitset的size小,则在前面用0补充(如上面的栗子);若比bitsize大,参数为整数时取后面部分,参数为字符串时取前面部分(如下

#include<iostream>
#include<bitset>
#include<cstring>
using namespace std;
int main()
{
    bitset<2> bitset1(12);//12的二进制为1100(长度为4),但bitset1的size=2,只取后面部分,即00
    string s="100101";
    bitset<4> bitset2(s);//s的size=6,而bitset的size=4,只取前面部分,即1001

    cout<<bitset1<<endl;
    cout<<bitset2<<endl;

/**
输出结果  
00
1001
*/

    return 0;
}

可用的操作符

bitset对于二进制有位操作符,具体如下

#include<iostream>
#include<bitset>
#include<cstring>
using namespace std;
int main()
{
    bitset<4> s1(string("1001"));
    bitset<4> s2(string("0011"));
    cout<<(s1^s2)<<endl;//1010  异或之后的值
    cout<<(s1&s2)<<endl;//0001  与之后的值
    cout<<(s1|s2)<<endl;//1011
    cout<<(s1<<2)<<endl;//0100  左移两位 右边补0
    cout<<(s1>>2)<<endl;//0010  右移两位 左边补0
    cout<<(~s1)<<endl;//0110  按位取反
    cout<<(s1!=s2)<<endl;//1  不等
    

    return 0;
}

此外,可以通过 [ ] 访问元素(类似数组),注意最低位下标为0,如下:

#include<iostream>
#include<bitset>
#include<cstring>
using namespace std;
int main()
{
    bitset<4> s1(string("1011"));
    cout<<s1[0]<<endl;
    cout<<s1[1]<<endl;
    cout<<s1[2]<<endl;

    /**
    输出 110
    */
    return 0;
}

当然,通过这种方式对某一位元素赋值也是可以的,栗子就不放了。

可用函数

bitset还支持一些有意思的函数,比如:

#include<iostream>
#include<bitset>
#include<cstring>
using namespace std;
int main()
{
    bitset<4> s1(string("1011"));
    cout<<s1.count()<<endl;//3  统计1的个数
    cout<<s1.size()<<endl;//4  大小

    cout<<s1.test(1)<<endl;//1  判断下标为1的位置是不是1
    cout<<s1.test(2)<<endl;//0

    cout<<s1.any()<<endl;//是否含有1
    cout<<s1.none()<<endl;//0  是否没有1
    cout<<s1.all()<<endl;//是否全为1

    return 0;
}

补充说明一下:test函数会对下标越界作出检查,而通过 [ ] 访问元素却不会经过下标检查,所以,在两种方式通用的情况下,选择test函数更安全一些

另外,含有一些函数:

 

#include<iostream>
#include<bitset>
#include<cstring>
using namespace std;
int main()
{
    bitset<4> s1(string("1011"));

    cout<<s1.flip(0)<<endl;//1010  0位取反
    cout<<s1.flip()<<endl;//0101  默认情况 每一位都取反
    cout<<s1.set()<<endl;//全部赋值为1
    cout<<s1.set(2,0)<<endl;//两个参数时 将下标为第一个参数的数 值改为第二个参数的值
    cout<<s1.set(2)<<endl;//一个参数时 下标为这一位的数值改为1
    cout<<s1.reset(0)<<endl;//小标为0的数 值改为0
    cout<<s1.reset()<<endl;//全部变为0
    

    return 0;
}

同样,它们也都会检查下标是否越界,如果越界就会抛出异常

最后,还有一些类型转换的函数,如下:

#include<iostream>
#include<bitset>
#include<cstring>
using namespace std;
int main()
{
    bitset<4> s1(string("1011"));

    string s=s1.to_string();//转化为字符换类型
    cout<<s<<endl;

    unsigned long b=s1.to_ulong();//11  转化为unsigned long
    cout<<b<<endl;
    return 0;
}
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/10792806.html