标签 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
有没有源码或者类似的源码?
谢谢!
最佳答案
这并不难。
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); }
关于c - 在 linux C 中寻找位图实现 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16947492/
相关文章:
C OR 没有循环的大内存块
C 字符串在超过 7 个字符时表现异常
C 堆分配索引/访问
java - 使用 Volley 在 Android 中调用带有字符串参数的 API
php - 使用 laravel 使用我自己的 api 无法获取参数
c - "Segmentation fault 11"for 循环崩溃
node.js - 如何让域在特定端口下工作?
linux - 仅 Rsync 较新的文件和文件夹
linux - 在 bash 脚本中执行 cut 命令
javascript - Vimeo API 无法处理多个视频