Bit functions

Working with bit arrays

These functions allow you to work with variables as if they were arrays of bits:

bool getbit(int a, int i) – returns bit a[i]

void setbit(int a, int i) – performs a[i] = 1

void clrbit(int a, int i) – performs a[i] = 0

void tglbit(int a, int i) – performs a[i] = !a[i]

Unsetting bit flags

void notflag(int flags, int flag)

Bit flags can be applied using flags & SOMEFLAG. But removing them requires a more complicated expression !(flags & SOMEFLAG).

notflag(flags, SOMEFLAG) unsets SOMEFLAG, shorthand for !(flags & SOMEFLAG).