2 * UCW Library -- Bit Array Operations
4 * (c) 2003--2004 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
12 typedef u32 *bitarray_t;
13 #define BIT_ARRAY_WORDS(n) (((n)+31)/32)
14 #define BIT_ARRAY_BYTES(n) (4*BIT_ARRAY_WORDS(n))
15 #define BIT_ARRAY(name,size) u32 name[BIT_ARRAY_WORDS(size)]
18 bit_array_zero(bitarray_t a, uns n)
20 bzero(a, BIT_ARRAY_BYTES(n));
24 bit_array_set_all(bitarray_t a, uns n)
26 memset(a, 255, BIT_ARRAY_BYTES(n));
30 bit_array_set(bitarray_t a, uns i)
32 a[i/32] |= (1 << (i%32));
36 bit_array_clear(bitarray_t a, uns i)
38 a[i/32] &= ~(1 << (i%32));
42 bit_array_isset(bitarray_t a, uns i)
44 return a[i/32] & (1 << (i%32));
48 bit_array_get(bitarray_t a, uns i)
50 return !! bit_array_isset(a, i);
54 bit_array_test_and_set(bitarray_t a, uns i)
56 uns t = bit_array_isset(a, i);
62 bit_array_test_and_clear(bitarray_t a, uns i)
64 uns t = bit_array_isset(a, i);
65 bit_array_clear(a, i);
69 /* Iterate over all set bits, possibly destructively */
70 #define BIT_ARRAY_FISH_BITS_BEGIN(var,ary,size) \
71 for (uns var##_hi=0; var##_hi < BIT_ARRAY_WORDS(size); var##_hi++) \
72 for (uns var##_lo=0; ary[var##_hi]; var##_lo++) \
73 if (ary[var##_hi] & (1 << var##_lo)) \
75 uns var = 32*var##_hi + var##_lo; \
76 ary[var##_hi] &= ~(1 << var##_lo); \
79 #define BIT_ARRAY_FISH_BITS_END \