본문으로 바로가기

Bit Operation

category Devlogs 2024. 3. 8. 10:10

프로그램의 메모리 점유율을 줄이는 목적으로..변수 하나로 수개에서 수십개의 상태를 저장하기 위해서..bit operation이 필요함..

#define BIT_SET(a,b) ((a) |= (1ULL<<(b)))
#define BIT_CLEAR(a,b) ((a) &= ~(1ULL<<(b)))
#define BIT_FLIP(a,b) ((a) ^= (1ULL<<(b)))
#define BIT_CHECK(a,b) (!!((a) & (1ULL<<(b))))        // '!!' to make sure this returns 0 or 1
 
void main()
{
    char v = 0;
    BIT_SET(v,0);
    printf("%d\n", v);
    BIT_SET(v,1);
    printf("%d\n", v);
    printf("%d\n", BIT_CHECK(v,0));
    printf("%d\n", BIT_CHECK(v,2));
    BIT_CLEAR(v,1);
    printf("%d\n", v);
}

Korea Tcl/Tk Community
블로그 이미지 ihmin 님의 블로그
VISITOR 오늘 / 전체