zoukankan      html  css  js  c++  java
  • C++之bitset

     1 #include <iostream>
     2 #include <bitset>
     3 #include <string>
     4 
     5 using  namespace std;
     6 //bitset类型处理二进制
     7 int main()
     8 {
     9     //bitset类型的变量a <>内的参数表示的是长度
    10     bitset<32> a;
    11     cout<<a<<endl;
    12     bitset<16> b=0xffff; //一个16进制等于4个二进制
    13     cout<<b<<endl;
    14     string s("10011001010");
    15     bitset<32> c(s,5,4);//从第五位开始初始化四个,从右向左数剩余补0
    16     cout<<c<<endl;
    17 
    18     //操作
    19     bool is_set=b.any(); //如果存在1就返回true
    20     bool is_notset=a.none();//如果全都是0返回true
    21     size_t bits_set=b.count();//返回b里面1的个数
    22     size_t bits_set1=b.size()-b.count(); //减法实现b里面0的个数
    23     cout<<bits_set1<<endl;
    24     a.set(); //将a中的0全部变为1
    25     cout<<a<<endl;
    26     a.reset();//将a中的1全部变为0
    27     cout<<a<<endl;
    28     a.flip();//反转 1-0 0-1
    29     a.flip(8);//某一位反转
    30     //
    31 
    32     unsigned long x=a.to_ulong(); //转换成10进制
    33     cout<<x<<endl;
    34 
    35     //
    36 
    37     return 0;
    38 }
  • 相关阅读:
    六个月的实习
    cookbook学习第二弹
    cookbook学习第一弹
    maketrans translate
    Python strip函数用法小结
    【翻译】How To Tango With Django 1.5.4 第一章
    os相关方法总结
    python基础(一)
    bash快捷键
    Linux基本命令
  • 原文地址:https://www.cnblogs.com/yh2924/p/12523432.html
Copyright © 2011-2022 走看看