aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--openbsc/include/openbsc/bitvec.h3
-rw-r--r--openbsc/src/bitvec.c16
2 files changed, 19 insertions, 0 deletions
diff --git a/openbsc/include/openbsc/bitvec.h b/openbsc/include/openbsc/bitvec.h
index a365e2a2d..b35aebf16 100644
--- a/openbsc/include/openbsc/bitvec.h
+++ b/openbsc/include/openbsc/bitvec.h
@@ -42,6 +42,9 @@ struct bitvec {
/* check if the bit is 0 or 1 for a given position inside a bitvec */
enum bit_value bitvec_get_bit_pos(struct bitvec *bv, unsigned int bitnr);
+/* get the Nth set bit inside the bit vector */
+unsigned int bitvec_get_nth_set_bit(struct bitvec *bv, unsigned int n);
+
/* Set a bit at given position */
int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum,
enum bit_value bit);
diff --git a/openbsc/src/bitvec.c b/openbsc/src/bitvec.c
index 8c82b8776..ad03958f3 100644
--- a/openbsc/src/bitvec.c
+++ b/openbsc/src/bitvec.c
@@ -77,6 +77,22 @@ enum bit_value bitvec_get_bit_pos(struct bitvec *bv, unsigned int bitnr)
return ZERO;
}
+/* get the Nth set bit inside the bit vector */
+unsigned int bitvec_get_nth_set_bit(struct bitvec *bv, unsigned int n)
+{
+ unsigned int i, k = 0;
+
+ for (i = 0; i < bv->data_len*8; i++) {
+ if (bitvec_get_bit_pos(bv, i) == ONE) {
+ k++;
+ if (k == n)
+ return i;
+ }
+ }
+
+ return 0;
+}
+
/* set the bit at a given position inside a bitvec */
int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnr,
enum bit_value bit)