STL bitset

本文深入解析了C++中位操作符和位集类的应用,包括如何利用位操作符进行位运算,如何创建并操作位集类,以及通过实例展示了位操作在实际编程中的高效应用。
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<string>
 5 #include<bitset>
 6 using namespace std;
 7 
 8 int main( )
 9 {
10     string str = "10101010";   
11     bitset<10>cha(str,str.size() - 5);   // 将最后几个元素赋值进去;
12     cout<<cha<<endl;
13     bitset<100>temp(20); // 建立一个 容器  初始化为 20
14     temp.set();         // 全部化为1;
15     temp.reset();       // 全部化为0;
16     temp.set(1);        // 指定位置化为1;
17     temp.reset(2);      // 指定位置化为0;
18     temp.flip();        // 全部反转
19     temp.flip(1);       // 指定位置反转
20     cout<<temp[1]<<endl;// 获取 1 位置的 二进制数
21     cout<<temp.test(1)<<endl;       // 判断 1 位置的 二进制数 是否为1
22     cout<<temp.count()<<endl;       // 二进制为 1 的个数
23     cout<<temp.size()<<endl;        // 二进制位   的个数
24     cout<<temp.any()<<endl;         // 判断是否存在存在二进制为1的位置
25     temp = temp<<10;                // 二进制进行右移动
26     temp = temp^temp;               // 进行亦或运算;
27     cout<<temp<<endl;
28     cout<<temp.to_ulong()<<endl;    // 讲结果  以 无符号长整型返回
29     return 0;
30 }

 

转载于:https://www.cnblogs.com/wulangzhou/archive/2013/03/13/2958189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值