首页 > 其他 > 详细

用二进制位表示状态,从而将状态压缩到一个整数里表示

时间:2015-07-22 18:09:19      阅读:236      评论:0      收藏:0      [点我收藏+]
#include <iostream>
using namespace std;

const int STATUS_SIZE = 30;

int main()
{
    int status = 0;

    //set status : set the fifth position is true
    status = status | 1 << 4;
    cout << status << endl;

    //test if the fifth position is true
    if (status >> 4 && 1)
        cout << "yes" << endl;
    else
        cout << "no" << endl;

    //test if all the position is true
    if (status == (1 << STATUS_SIZE - 1))
        cout << "yes" << endl;
    else
        cout << "no" << endl;

    
    status = status | 1 << 2;
    cout << status << endl;

    //unset status : set the fifth position is false
    status = status & ~(1 << 4);
    cout << status << endl;



}

 

用二进制位表示状态,从而将状态压缩到一个整数里表示

原文:http://www.cnblogs.com/scarecrow-blog/p/4668035.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!