From cd58af7f7c6b6da08c4f34e25882752db3e71d48 Mon Sep 17 00:00:00 2001 From: orange Date: Sun, 9 Jun 2013 20:56:25 +0200 Subject: [PATCH] add bit fuckery --- Makefile | 5 ++++- bit-fuckery.c | 33 +++++++++++++++++++++++++++++++++ bit-fuckery2.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 bit-fuckery.c create mode 100644 bit-fuckery2.c diff --git a/Makefile b/Makefile index f62d539..24297e5 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/bit-fuckery.c b/bit-fuckery.c new file mode 100644 index 0000000..06ea2e9 --- /dev/null +++ b/bit-fuckery.c @@ -0,0 +1,33 @@ +#include + +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); +} diff --git a/bit-fuckery2.c b/bit-fuckery2.c new file mode 100644 index 0000000..855019b --- /dev/null +++ b/bit-fuckery2.c @@ -0,0 +1,37 @@ +#include + +#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); +}