I recently started cleaning up my projects and decided that I want to create a personal repository of useful bits of code. I found this bitmap code between them and heavily edited it and I'd appreciate if someone could review it, because I'm not that used to bitwise operations and such. Is the code portable enough, or there should be changes?
bitmap.h
#ifndef BITMAP__
#define BITMAP__
#define BIT (8*sizeof(byte))
#define BITMAP_NOTFOUND -1
typedef enum{false=0, true} bool;
typedef unsigned char byte;
bool bitmapGet (byte *, int);
void bitmapSet (byte *, int);
void bitmapReset (byte *, int);
int bitmapSearch(byte *, bool, int, int);
#endif
bitmap.c
#include "bitmap.h"
static bool get (byte, byte);
static void set (byte *, byte);
static void reset(byte *, byte);
/* CAREFUL WITH pos AND BITMAP SIZE! */
bool bitmapGet(byte *bitmap, int pos) {
/* gets the value of the bit at pos */
return get(bitmap[pos/BIT], pos%BIT);
}
void bitmapSet(byte *bitmap, int pos) {
/* sets bit at pos to 1 */
set(&bitmap[pos/BIT], pos%BIT);
}
void bitmapReset(byte *bitmap, int pos) {
/* sets bit at pos to 0 */
reset(&bitmap[pos/BIT], pos%BIT);
}
int bitmapSearch(byte *bitmap, bool n, int size, int start) {
/* Finds the first n value in bitmap after start */
/* size is the Bitmap size in bytes */
int i;
/* size is now the Bitmap size in bits */
for(i = start+1, size *= BIT; i < size; i++)
if(bitmapGet(bitmap,i) == n)
return i;
return BITMAP_NOTFOUND;
}
static bool get(byte a, byte pos) {
/* pos is something from 0 to 7*/
return (a >> pos) & 1;
}
static void set(byte *a, byte pos) {
/* pos is something from 0 to 7*/
/* sets bit to 1 */
*a |= 1 << pos;
}
static void reset(byte *a, byte pos) {
/* pos is something from 0 to 7*/
/* sets bit to 0 */
*a &= ~(1 << pos);
}