-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitSet.c
95 lines (84 loc) · 2.03 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdlib.h>
#include <string.h>
#include "bitSet.h"
uint8_t* createBitset(size_t n) {
uint8_t* bitset;
if (n % 8 == 0) {
bitset = calloc((n / 8), sizeof(uint8_t));
} else {
bitset = calloc((n / 8 + 1), sizeof(uint8_t));
}
return bitset;
}
uint8_t* copyBitset(uint8_t* bitset, size_t n) {
uint8_t* copy = createBitset(n);
size_t nBytes = (n % 8 == 0) ? (n / 8) : (n / 8 + 1);
memcpy(copy, bitset, nBytes);
return copy;
}
void destroyBitset(uint8_t* bitset) {
free(bitset);
}
void setBitTrue(uint8_t* bitset, size_t i) {
bitset[i / 8] |= (1 << (i % 8));
}
void setBitFalse(uint8_t* bitset, size_t i) {
bitset[i / 8] &= ~(1 << (i % 8));
}
void setBit(uint8_t* bitset, size_t i, uint8_t value) {
if (value) {
setBitTrue(bitset, i);
} else {
setBitFalse(bitset, i);
}
}
uint8_t getBit(uint8_t* bitset, size_t i) {
return (bitset[i / 8] & (1 << (i % 8))) ? 1 : 0;
}
void printBitset(uint8_t* bitset, size_t n, FILE* out) {
fprintf(out, "[");
for (size_t i=0; i<n; ++i) {
fprintf(out, "%i", getBit(bitset, i));
}
fprintf(out, "]\n");
}
void bitsetUnion(uint8_t* a, uint8_t* b, size_t n) {
int i = (n % 8) ? n / 8 : n / 8 - 1;
for ( ; i>=0; --i) {
a[i] |= b[i];
}
}
void bitsetIntersection(uint8_t* a, uint8_t* b, size_t n) {
int i = (n % 8) ? n / 8 : n / 8 - 1;
for ( ; i>=0; --i) {
a[i] &= b[i];
}
}
// int main(int argc, char** argv) {
// uint8_t* bitset = createBitset(16);
// uint8_t* b = createBitset(16);
// uint8_t* c = createBitset(16);
// uint8_t* d;
// printBitset(bitset, 16, stdout);
// // printStrange(bitset, 2);
// setBitTrue(bitset, 5);
// setBitTrue(bitset, 8);
// printBitset(bitset, 16, stdout);
// bitsetUnion(b, bitset, 16);
// printBitset(b, 16, stdout);
//
// setBitFalse(bitset, 5);
// printBitset(bitset, 16, stdout);
// bitsetUnion(c, bitset, 16);
// d = copyBitset(c, 16);
//
// printBitset(c, 16, stdout);
// printBitset(d, 16, stdout);
//
// bitsetUnion(c, b, 16);
// printBitset(c, 16, stdout);
//
// destroyBitset(bitset);
// destroyBitset(b);
// destroyBitset(c);
// }