Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

looking for a bitmap implementation API in linux C [closed]

Tags:

c

linux

bitmap

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!

like image 496
misteryes Avatar asked Mar 12 '26 16:03

misteryes


1 Answers

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);
}
like image 100
Paul Hankin Avatar answered Mar 14 '26 05:03

Paul Hankin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!