c-exercises/bit-fuckery2.c
2013-06-09 20:56:25 +02:00

37 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);
}