From bd24a0b7782960c0ad9e41ea9e35ace21e2d0f6c Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 4 Feb 2017 23:08:52 +0100 Subject: [PATCH] Add a routine to count the number of set bits in a bitset. --- modules/src/data/bitmap.c | 27 +++++++++++++++++++++++++++ modules/src/data/bitmap.h | 1 + 2 files changed, 28 insertions(+) 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