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

#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;



}
原文地址:https://www.cnblogs.com/scarecrow-blog/p/4668035.html