1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#ifndef _BITSET_H
#define _BITSET_H
#include <stdlib.h>
typedef int * BITSET;
static inline void bit_set(BITSET bs,int num)
{
bs[num/(8*sizeof(int))]|=1<<(num%(8*sizeof(int)));
}
static inline int bit_check(BITSET bs,int num)
{
return bs[num/(8*sizeof(int))]&1<<(num%(8*sizeof(int)));
}
// use free() when done. returns NULL on bad_alloc
static inline BITSET bitset_new(int size)
{
return (BITSET)calloc(1,((size+8*sizeof(int)-1)&~(8*sizeof(int)-1))/8);
}
static inline int bits_used(BITSET bits,int size) // {{{ returns true if any bit is used
{
size=(size+8*sizeof(int)-1)/(8*sizeof(int));
while (size>0) {
if (*bits) {
return 1;
}
bits++;
size--;
}
return 0;
}
// }}}
#endif
|