在linux C中寻找位图实现API

mis*_*yes 1 c linux api bitmap

我想在Linux C中使用位图API.

我需要2 ^ 18位,所以它需要32KB内存.我会经常在位图中设置和取消设置位.

所以基本上我需要API,如:

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
Run Code Online (Sandbox Code Playgroud)

有没有源代码或类似的源代码?

谢谢!

Pau*_*kin 8

这并不困难.

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);
}
Run Code Online (Sandbox Code Playgroud)