-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitarray.c
72 lines (60 loc) · 1.75 KB
/
bitarray.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
#include <stdio.h>
#include <stdlib.h>
typedef __uint32_t uint32_t;
struct BitArray {
uint32_t *array;
uint32_t size;
};
typedef struct BitArray BitArray;
BitArray *init(uint32_t size) {
BitArray *bitArray = (BitArray *)calloc(1, sizeof(BitArray));
uint32_t sizeOfArray = size/32 + (size % 32 == 0)? 0 : 1;
bitArray->array = (uint32_t *)calloc(sizeOfArray, 4);
return bitArray;
}
void set(BitArray *bitArray, uint32_t index, uint32_t val) {
uint32_t arrayIndex = index/32;
uint32_t bitMask = 1 << (index % 32);
if (val == 0) {
bitArray->array[arrayIndex] &= ~bitMask;
} else {
bitArray->array[arrayIndex] |= bitMask;
}
}
uint32_t get(BitArray *bitArray, uint32_t index) {
uint32_t arrayIndex = index/32;
uint32_t bitMask = 1 << (index % 32);
return (bitArray->array[arrayIndex] & bitMask) >> (index % 32);
}
void checkError(int expected, int output, uint32_t *failed) {
if (expected != output) {
printf("Expected %d but got %d\n", expected, output);
(*failed)++;
}
}
void test() {
uint32_t failed = 0;
BitArray *bitArray = init(10);
set(bitArray, 4, 1);
set(bitArray, 7, 1);
checkError(1, get(bitArray, 4), &failed);
checkError(1, get(bitArray, 7), &failed);
checkError(0, get(bitArray, 23), &failed);
free(bitArray);
bitArray = init(40);
set(bitArray, 10, 1);
set(bitArray, 34, 1);
checkError(1, get(bitArray, 10), &failed);
checkError(1, get(bitArray, 34), &failed);
checkError(0, get(bitArray, 4), &failed);
free(bitArray);
if (failed != 0) {
printf("Failed %d tests\n", failed);
} else {
printf("PASSED ALL TESTS!\n");
}
}
int main(int argc, char *argv[]) {
test();
return 0;
}