2 * UCW Library -- Support routines for bitarray
4 * (c) 2012 Pavel Charvat <pchar@ucw.cz>
5 * (c) 2013 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include <ucw/bitops.h>
13 #include <ucw/bitarray.h>
15 uns bit_array_count_bits(bitarray_t a, uns n)
18 n = BIT_ARRAY_WORDS(n);
24 bitarray_t bit_array_xrealloc(bitarray_t a, uns old_n, uns new_n)
26 uns old_bytes = BIT_ARRAY_BYTES(old_n);
27 uns new_bytes = BIT_ARRAY_BYTES(new_n);
28 if (old_bytes == new_bytes)
30 a = xrealloc(a, new_bytes);
31 if (old_bytes < new_bytes)
32 bzero(a + old_bytes, new_bytes - old_bytes);
44 bitarray_t a = alloca(BIT_ARRAY_BYTES(sizeof(buf)));
47 if (!fgets(buf, sizeof(buf), stdin))
50 for (n = 0; buf[n] == '0' || buf[n] == '1'; n++);
52 for (uns i = 0; i < n; i++)
55 printf("%u\n", bit_array_count_bits(a, n));