bitset的一些操作总结

Xredman posted @ Jun 02, 2009 08:53:42 AM in 以前博文 , 1365 阅读

 

/******************************************************************
** 文件名:bitset.cpp
** Copyright (c) 2009 *********
** 创建人: Xredman
** 日期: 6.2
** 修改人:
** 日期:
** 描述: 本程序实现对bitset的一些操作,主要用于熟悉位操作
**
** 版本:
**--------------------------------------------------------------------------
---
******************************************************************/


#include <iostream>
#include <bitset>
#include <string>
using namespace std;

int main()
{
        /*-----声明-----*/

        bitset<32> bt1;
        /*位的计数从0开始,即0~31
          缺省情况下,所有的位都被初始化为0
        */


        bitset<32> bit2(0xffff);//位构造函数显式提供一个无符号参数

        string bitval("1010");
        bitset<32> bit3(bitval);

        if(bt1.any())//当bitset对象含有一位或多位被置为1时,返回true
                cout<<"初始化是含有1."<<endl;
        else if(bt1.none())//当bitset对象不含有1时,返回true
                cout<<"初始化是不含1."<<endl;

        /*--------置位操作----------*/

        bt1[26] = 1;//将26位设置为1
        bt1.set(27);//将27位设置为1

        bt1[26] = 0;//将26位设置为0
        bt1.reset(26);//将26位设置为0

        /*全部置位
        bt1.set();
        bt1.reset();*/


        cout<<"被置为1的位数有:"<<bt1.count()<<endl;
        //返回被置为1的的位的个数

        if(bt1.test(27))//或者用下标操作符bt1[27]
                cout<<"第27位被置为1."<<endl;

        /*-----翻转操作-----*/

        bt1.flip(0);//翻转第0位
        bt1[1].flip();//翻转第1位
        bt1.flip();//全部翻转

        /*-----类型转化-----*/

        string sbt = bt1.to_string();
        unsigned long lbt = bt1.to_ulong();
       

        return 0;
}
 





  • 无匹配
  • 无匹配
Avatar_small
MasterLuo 说:
Jun 04, 2009 03:35:17 AM

位操作真的是一个神奇的东西.

pavzi.com 说:
Jan 19, 2024 05:58:00 AM

The primary idea or goal of this website has been to offer resources that contain comprehensive information on every subject and are accessible via the Internet. making certain that each and every reader finds the most relevant and pavzi.com worthwhile information regarding the subject they are searching for and linking to our content.Because our website is multi-niche or multi-category, it will guarantee that it offers resources and information on every subject. Our website features a number of timeless topics, including career, job recruitment, education, technology, and reviews, among others. Indeed, Tech, Finance, and Product Reviews are our primary focus.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter