• C++:bitset类的使用


     1 #include <iostream>
     2 #include <bitset>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     //初始化一个bitmap , 将所有位置位
     9     bitset<16>    bit(0xFFFF);
    10 
    11     cout << "construct" <<endl;
    12     cout << bit.to_string() << endl;
    13 
    14     //复位,将第12位置为false , 即0
    15     bit.reset(12);
    16     cout << " reset 12" << endl;
    17     cout << bit.to_string() << endl;
    18 
    19     //复位,将所有位置为false
    20     bit.reset();
    21     cout << " reset all" << endl;
    22     cout << bit.to_string() << endl;
    23 
    24     //翻转 , 将第2位反转
    25     bit.flip(2);
    26     cout << " flip 2" << endl;
    27     cout << bit.to_string() << endl;
    28 
    29     //反转,将所有位反转
    30     bit.flip();
    31     cout << bit.to_string() << endl;
    32 
    33     cout << bit.to_ulong() <<endl;
    34 
    35     //检测是否所有位都没被置位
    36     cout << (bit.none()?"没有任何位被置位":"存在某位被置位") << endl;
    37     //检测是否存在某位被置位
    38     cout << (bit.any()?"存在某位被置位":"没有任何位被置位") << endl;
    39     //检测某一位是否被置位
    40     cout << "第2位" <<(bit.test(2)?"":"") << "置位" << endl;
    41 
    42     return 0;
    43 }
  • 相关阅读:
    推荐系统:协同过滤基础
    roslyn
    动态生成wcf 服务端
    p2p协议
    Mybatis源码浅读一
    Django 使用es实现全文检索
    mac下gitlab鉴权失败修改密码
    Mako 模板用法
    fastapi 操作数据库
    mysql多表查询学习
  • 原文地址:https://www.cnblogs.com/wowk/p/3219752.html
Copyright © 2020-2023  润新知