libds

a collection of simple data structures
Log | Files | Refs | LICENSE

bitmap.c (636B)


      1 #include <stdlib.h>
      2 
      3 struct bitmap {
      4 	unsigned long long map;
      5 };
      6 
      7 int
      8 bitmapset(struct bitmap *bm, unsigned long long bit)
      9 {
     10 	if (bit > (sizeof(bm->map) * 8 - 1))
     11 		return 1;
     12 	bm->map |= (1ULL << bit);
     13 	return 0;
     14 }
     15 
     16 int
     17 bitmapclr(struct bitmap *bm, unsigned long long bit)
     18 {
     19 	if (bit > (sizeof(bm->map) * 8 - 1))
     20 		return 1;
     21 	bm->map &= ~(1ULL << bit);
     22 	return 0;
     23 }
     24 
     25 int
     26 bitmaptest(struct bitmap *bm, unsigned long long bit)
     27 {
     28 	if (bit > (sizeof(bm->map) * 8 - 1))
     29 		return -1;
     30 	return bm->map & (1ULL << bit);
     31 }
     32 
     33 struct bitmap *
     34 bitmapinit(void)
     35 {
     36 	struct bitmap *bm;
     37 
     38 	bm = malloc(sizeof(*bm));
     39 	if (!bm)
     40 		return NULL;
     41 	return bm;
     42 }