-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitset.c
48 lines (42 loc) · 852 Bytes
/
bitset.c
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
46
47
48
#include "bitset.h"
Bitset BitsetCtor(int n)
{
Bitset b;
b.words = (n + 63) / 64;
b.buf = malloc(b.words * sizeof(u64));
b.bits = n;
clearBitset(&b);
return b;
}
void BitsetDtor(Bitset* b)
{
free(b->buf);
}
int getBit(Bitset* b, int index)
{
assert(index >= 0 && index < b->bits);
u64 mask = 1ULL << (index % 64);
return (b->buf[index / 64] & mask) ? 1 : 0;
}
void setBit(Bitset* b, int index, int value)
{
assert(index >= 0 && index < b->bits);
u64 mask = 1ULL << (index % 64);
int word = index / 64;
b->buf[word] &= ~mask;
b->buf[word] |= value ? mask : 0;
}
void clearBitset(Bitset* b)
{
memset(b->buf, 0, b->words * sizeof(u64));
}
int bitsetPopcnt(Bitset* b)
{
//could make faster but probably not necessary
int pop = 0;
for(int i = 0; i < b->bits; i++)
{
pop += getBit(b, i);
}
return pop;
}