비트연산자를 통한 프로퍼티 구현

2008/02/29 22:53
자주 확인하지 않는 프로퍼티들이 bool 값으로 들어있는것은 아깝다. 이때는 비트연산자를 이용해서 프로퍼티들 관리하는것이 좋다.

(Language : cpp)
enum PROP
{
    PROP_1 = 1 << 1,
    PROP_2 = 1 << 2
};

int g_prop = 0;

void AddProperty(PROP _eProp)
{
    g_prop |= _eProp;
}

void RemoveProperty(PROP _eProp)
{
    g_prop &= ~_eProp;
}

bool QueryProperty(PROP _eProp)
{
    if( (g_prop & _eProp) == _eProp )
        return true;
    return false;
    // 혹은
    // return (g_prop & _eProp) ? true : false;
}

프로그래밍 ,

Trackback Address:http://poksion.cafe24.com/poklog/trackback/94