aboutsummaryrefslogtreecommitdiff
path: root/src/bitfield.c
blob: 9a42926c7a9d7ae145e518b30eb19da1f9058550 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "bitfield.h"


void prne_bf_set (uint8_t *bf, const unsigned int bit, const bool v) {
	const unsigned int p = bit / 8;
	const unsigned int s = bit - p * 8;

	if (v) {
		bf[p] |= 1 << s;
	}
	else {
		bf[p] &= ~(1 << s);
	}
}

bool prne_bf_test (
	const uint8_t *bf,
	const size_t size,
	const unsigned int bit)
{
	const unsigned int p = bit / 8;
	const unsigned int s = bit - p * 8;

	if (size <= p) {
		return false;
	}

	return (bf[p] & (1 << s)) != 0;
}

void prne_bf_foreach (
	void *ctx,
	const uint8_t *bf,
	const size_t size,
	prne_bf_foreach_ft f)
{
	unsigned int bit = 0;

	for (size_t i = 0; i < size; i += 1) {
		for (unsigned int j = 0; j < 8; j += 1) {
			f(ctx, bit, (bf[i] & (1 << j)) != 0);
			bit += 1;
		}
	}
}