码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-22 18:12:15      阅读:95      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!