diff options
Diffstat (limited to 'src/bitset.c')
| -rw-r--r-- | src/bitset.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/bitset.c b/src/bitset.c new file mode 100644 index 0000000..0d2f66a --- /dev/null +++ b/src/bitset.c @@ -0,0 +1,85 @@ +#include "bitset.h" + +void +bitarena_init(bitarena_t *arena, int size) +{ + arena->size = size; + size_t memSize = sizeof(char) * BITSET_BYTES * size; + arena->arena = malloc(memSize); + memset(arena->arena, 0, memSize); +} + +bitset_t +bitset_create() +{ + bitset_t tmp = (bitset_t) malloc(sizeof(char) * BITSET_BYTES); + memset(tmp, 0, sizeof(char) * BITSET_BYTES); + return tmp; +} + +bitset_t +bitarena_at(bitarena_t *arena, int idx) +{ + return (arena->arena) + (idx * BITSET_BYTES); +} + +bitset_t +bitset_and(bitset_t b1, bitset_t b2) +{ + bitset_t bitAnd = bitset_create(); + + for (int i = 0; i < BITSET_BYTES; ++i) + { + bitAnd[i] = b1[i] & b2[i]; + } + + return bitAnd; +} + +int +bitset_isZero(bitset_t bitset) +{ + for (int i = 0; i < BITSET_BYTES; ++i) + { + if (bitset[i] != 0) + return 0; + } + return 1; +} + +void +bitset_set(bitset_t bitset, int position) +{ + int byte = position / (sizeof(char) * 8); + int shift = position % (sizeof(char) * 8); + bitset[byte] = bitset[byte] | (1 << shift); +} + +void +bitset_unset(bitset_t bitset, int position) +{ + int byte = position / (sizeof(char) * 8); + int shift = position % (sizeof(char) * 8); + bitset[byte] = bitset[byte] & ~(1 << shift); +} + +int +bitset_equals(bitset_t b1, bitset_t b2) +{ + for (int i = 0; i < BITSET_BYTES; ++i) + { + if (b1[i] != b2[i]) + return 0; + } + return 1; +} + +void +bitset_print(bitset_t bitset) +{ + for (int i = 3; i >= 0; --i) + { + printf("%i ", bitset[i]); + } + printf("\n"); +} |