From: David Given Date: Sat, 4 Feb 2017 22:08:52 +0000 (+0100) Subject: Add a routine to count the number of set bits in a bitset. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=bd24a0b7782960c0ad9e41ea9e35ace21e2d0f6c;p=ack.git Add a routine to count the number of set bits in a bitset. --- diff --git a/modules/src/data/bitmap.c b/modules/src/data/bitmap.c index d47684d9d..dff7db163 100644 --- a/modules/src/data/bitmap.c +++ b/modules/src/data/bitmap.c @@ -85,3 +85,30 @@ int bitmap_find_unset_bit(unsigned int* bitmap, int size) return -1; } + +static int count_bits_in_word(unsigned int word) +{ + int i = 0; + while (word) + { + if (word & 1) + i++; + word >>= 1; + } + return i; +} + +int bitmap_count_set_bits(unsigned int* bitmap, int size) +{ + int word; + int words = ((size-1) / BITS_PER_WORD) + 1; + int count = 0; + + for (word=0; word