-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap.c
43 lines (33 loc) · 925 Bytes
/
bitmap.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
#include <stdint.h>
#include <stdio.h>
#include "bitmap.h"
#define nth_bit_mask(n) (1 << (n))
#define byte_index(n) ((n) / 8)
#define bit_index(n) ((n) % 8)
// Get the given bit from the bitmap.
int bitmap_get(void *bm, int i) {
uint8_t *base = (uint8_t *) bm;
return (base[byte_index(i)] >> bit_index(i)) & 1;
}
// Set the given bit in the bitmap to the given value.
void bitmap_put(void *bm, int i, int v) {
uint8_t *base = (uint8_t *) bm;
long bit_mask = nth_bit_mask(bit_index(i));
if (v) {
base[byte_index(i)] |= bit_mask;
} else {
bit_mask = ~bit_mask;
base[byte_index(i)] &= bit_mask;
}
}
// Pretty-print the bitmap (with the given no. of bits).
void bitmap_print(void *bm, int size) {
for (int i = 0; i < size; i++) {
putchar(bitmap_get(bm, i) ? '1' : '0');
if ((i + 1) % 64 == 0) {
putchar('\n');
} else if ((i + 1) % 8 == 0) {
putchar(' ');
}
}
}