You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
629 B
C

#include <assert.h>
#define BIT_DEL(x, n) ((x) &= ~(1<<(n)))
#define BIT_SET(x, n) ((x) |= (1<<(n)))
#define BIT_TOGGLE(x, n) ((x) ^= (1<<(n)))
int main() {
int foo;
foo = 42;
/* Delete bit 5 */
BIT_DEL(foo, 5);
assert(foo == 10);
BIT_DEL(foo, 5);
assert(foo == 10);
/* Set bit 5 again */
BIT_SET(foo, 5);
assert(foo == 42);
BIT_SET(foo, 5);
assert(foo == 42);
/* Set bit 0 */
BIT_SET(foo, 0);
assert(foo == 43);
/* Delete bit 0 */
BIT_DEL(foo, 0);
assert(foo == 42);
/* Toggle bit 5 */
BIT_TOGGLE(foo, 5);
assert(foo == 10);
BIT_TOGGLE(foo, 5);
assert(foo == 42);
}