I want a bitmap API in linux C.
I need 2^18 bits, so it needs 32KB memory. and I will frequently set and unset bits in the bitmap.
so basically I need APIs like:
set_bitmap(int i) // it sets the i-th bit to 1 in the bitmap
unset_bitmap(int i) // it sets the i-th bit to 0 in the bitmap
bitmap_t create_bitmap(int n) // it creates a bitmap of size n, like n=2^18
are there any source code or similar source codes?
thanks!
This isn't difficult.
typedef unsigned char* bitmap_t;
void set_bitmap(bitmap_t b, int i) {
b[i / 8] |= 1 << (i & 7);
}
void unset_bitmap(bitmap_t b, int i) {
b[i / 8] &= ~(1 << (i & 7));
}
void get_bitmap(bitmap_t b, int i) {
return b[i / 8] & (1 << (i & 7)) ? 1 : 0;
}
bitmap_t create_bitmap(int n) {
return malloc((n + 7) / 8);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With