当前方向:
以 unsigned char 开头,在我的系统上使用 sizeof 是 1 字节。范围是 0-255。 如果长度是我需要的位数,那么元素是我的数组中需要的元素数(字节)。
constant unsigned int elements = length/8 + (length % y > 0 ? 1 : 0);
unsigned char bit_arr[elements];
现在我添加基本功能,例如设置、取消设置和测试。其中 j 是每字节索引的位数,i 是字节索引,h = 位索引。我们有 i = h/8 和 j = i % 8。
伪代码:
bit_arr[i] |= (1 << j); // Set
bit_arr[i] &= ~(1 << j); // Unset
if( bit_arr[i] & (1 << j) ) // Test
最佳答案
看来您非常清楚需要做什么。虽然不是 pow(2, j)
, 使用 1 << j
.您还需要更改您的 test
代码。您不希望测试对数组进行赋值。
关于c - 如何实现位数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5998589/