CFMutableBitVector::CFBitVectorSetAllBits 好像不行

CFMutableBitVector::CFBitVectorSetAllBits does not seem to work

我正在使用 CFMutableBitVector 来管理位集合,但是 CFBitVectorSetAllBits API 似乎没有按预期工作。为了说明问题,请考虑示例代码:

vector = CFBitVectorCreateMutable(kCFAllocatorDefault, 32);

printf("Setting all bits to 1\n");
CFBitVectorSetAllBits(vector, 1);

printf("Checking whether bit 0 is set... ");
isSet = CFBitVectorGetBitAtIndex(vector, 0);
if (isSet)
    printf("It is\n");
else
    printf("It is not\n");

我得到的输出是:

Setting all bits to 1
Checking whether bit 0 is set... It is not

不过,如果我使用 CFBitVectorSetBitAtIndex,可以很好地设置或清除各个位。我在这里错过了什么吗?任何指针将不胜感激!

CFBitVectorCreateMutable 函数创建 empty 向量,第二个参数指定向量的容量,即可以存储在其中的最大位数,但实际上并不向向量。您可以使用 CFBitVectorSetCount 函数向向量添加值:

CFMutableBitVectorRef v = CFBitVectorCreateMutable(kCFAllocatorDefault, 32);
CFBitVectorSetCount(v, 32); 
CFBitVectorSetAllBits(v, 1);