add bit fuckery

master
orange 12 years ago
parent 2e2a01e5ba
commit cd58af7f7c

@ -1,7 +1,7 @@
CFLAGS=-std=c99 -Wall -g -O2
INDENTOPTS=-kr --no-tabs --braces-on-func-def-line --indent-level2
TARGETS=approximate-pi linked-list mandelbrot mandelbrot.bmp threads circular-buffer structs
TARGETS=approximate-pi linked-list mandelbrot mandelbrot.bmp threads circular-buffer structs ncurses-pong bit-fuckery bit-fuckery2
.PHONY: all
all: $(TARGETS)
@ -22,3 +22,6 @@ mandelbrot.bmp: mandelbrot
threads: threads.c
$(CC) $(CFLAGS) -o $@ $< -pthread
ncurses-pong: ncurses-pong.c
$(CC) $(CFLAGS) -o $@ $< -lncurses

@ -0,0 +1,33 @@
#include <assert.h>
int main() {
int foo;
foo = 42;
/* Delete bit 5 */
foo &= ~(1 << 5);
assert(foo == 10);
foo &= ~(1 << 5);
assert(foo == 10);
/* Set bit 5 again */
foo |= (1 << 5);
assert(foo == 42);
foo |= (1 << 5);
assert(foo == 42);
/* Set bit 0 */
foo |= (1 << 0);
assert(foo == 43);
/* Delete bit 0 */
foo &= ~(1 << 0);
assert(foo == 42);
/* Toggle bit 5 */
foo ^= (1 << 5);
assert(foo == 10);
foo ^= (1 << 5);
assert(foo == 42);
}

@ -0,0 +1,37 @@
#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);
}
Loading…
Cancel
Save