Monday, 8 January 2007

Bitmaps - Part 1

I've heard of bitmaps but never seen an implementation. I guess I've not browsed enough code. Anyway I was thinking about it whilst watching "You, Me and Dupree", and thinking about it again as I ran to catch quiz night at the woody, and some more as we tried to work out what a Raider bar(twix) was called in Europe.

Anyway I just got home and implemented some nice and simple bitmap code. It should come in handy with keeping track of paging in the DB project. Enjoy. Oh and excuse the sloppy code, it's a first attempt and all.

#define bitmap_init(size) calloc(1, sizeof(char)*((size/8)+1))
#define bitmap_set(bitmap, i) bitmap[i/8]|=(1<<(i%8))
#define bitmap_unset(bitmap, i) bitmap[i/8]&=~(1<<(i%8))
#define bitmap_isset(bitmap, i)!!(bitmap[i/8]&(1<<(i%8)))

No comments: