Bit Math
.
<<info>> Some simple Bit Math. <</info>>
Source: stackoverflow.com
Setting a bit: number |= (1 << x); Clearing a bit: number &= ~(1 << x); Toggling a bit: number ^= (1 << x); Checking a bit: bit = number & (1 << x);
Source: stackoverflow.com
/* a=target variable, b=bit number to act upon 0-n */ #define BIT_SET(a,b) ((a) |= (1<<(b))) #define BIT_CLEAR(a,b) ((a) &= ~(1<<(b))) #define BIT_FLIP(a,b) ((a) ^= (1<<(b))) #define BIT_CHECK(a,b) ((a) & (1<<(b))) /* x=target variable, y=mask */ #define BITMASK_SET(x,y) ((x) |= (y)) #define BITMASK_CLEAR(x,y) ((x) &= ~(y)) #define BITMASK_FLIP(x,y) ((x) ^= (y)) #define BITMASK_CHECK(x,y) ((x) & (y))
Please log in to post comments.