add bit fuckery
This commit is contained in:
parent
2e2a01e5ba
commit
cd58af7f7c
3 changed files with 74 additions and 1 deletions
5
Makefile
5
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
|
||||
|
|
33
bit-fuckery.c
Normal file
33
bit-fuckery.c
Normal file
|
@ -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);
|
||||
}
|
37
bit-fuckery2.c
Normal file
37
bit-fuckery2.c
Normal file
|
@ -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…
Add table
Add a link
Reference in a new issue