From b7f0e00c65068b4f6920f359d3e652a951c7b5b9 Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 21 Jan 2017 20:50:57 +0100 Subject: [PATCH] Add a function for finding the first unset bit. --- modules/src/data/bitmap.c | 28 +++++++++++++++++++++++++++- modules/src/data/bitmap.h | 1 + 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/modules/src/data/bitmap.c b/modules/src/data/bitmap.c index 363d22d49..d47684d9d 100644 --- a/modules/src/data/bitmap.c +++ b/modules/src/data/bitmap.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "bitmap.h" @@ -35,7 +36,7 @@ int bitmap_findfirst(unsigned int* bitmap, int size) { unsigned int w = bitmap[word]; if (w) - return ffs(w) + word*BITS_PER_WORD; + return (ffs(w)-1) + word*BITS_PER_WORD; } return -1; @@ -59,3 +60,28 @@ void bitmap_or(unsigned int* dest, int size, unsigned int* src) dest[word] |= src[word]; } +int bitmap_find_unset_bit(unsigned int* bitmap, int size) +{ + int word; + int words = ((size-1) / BITS_PER_WORD) + 1; + + for (word=0; word