Objective c 如何比较两个位向量的等价性?
比较两个位向量最有效的方法是什么?在Objective-C中,我使用CbbitVectors并简单地比较两种方法中的每一位:Objective c 如何比较两个位向量的等价性?,objective-c,bit-manipulation,equivalent,bitvector,Objective C,Bit Manipulation,Equivalent,Bitvector,比较两个位向量最有效的方法是什么?在Objective-C中,我使用CbbitVectors并简单地比较两种方法中的每一位: for (CFIndex bitIndex = 0; bitIndex < numBits; bitIndex++) { if (CFBitVectorGetBitAtIndex(thisVector, bitIndex) != CFBitVectorGetBitAtIndex(thatVector, bitIndex)) { return
for (CFIndex bitIndex = 0; bitIndex < numBits; bitIndex++) {
if (CFBitVectorGetBitAtIndex(thisVector, bitIndex) != CFBitVectorGetBitAtIndex(thatVector, bitIndex)) {
return NO;
}
}
return YES;
for(CFIndex bitIndex=0;bitIndex
这很好,但我不确定是否有更有效的方法使用位运算符进行操作。可以对两个CffitVectorRef结构执行按位or操作,如中所述。但是,这在将来可能会失败,因为它依赖于实现。比较它们最安全的方法似乎是按照OP中的描述,一次只比较一位。下面是一个实用程序函数,它完成了全部工作:
BOOL CFBitVectorEqualsCFBitVector(CFBitVectorRef thisVector, CFBitVectorRef thatVector) {
CFIndex numBits = CFBitVectorGetCount(thisVector);
if (numBits != CFBitVectorGetCount(thisVector)) return NO;
for (CFIndex bitIndex = 0; bitIndex < numBits; bitIndex++) {
if (CFBitVectorGetBitAtIndex(thisVector, bitIndex) != CFBitVectorGetBitAtIndex(thatVector, bitIndex)) {
return NO;
}
}
return YES;
}
BOOL CfitVectorEqualsCfitVector(CfitVectorRef thisVector,CfitVectorRefThatVector){
CFIndex numBits=CFBitVectorGetCount(thisVector);
如果(numBits!=CfitVectorGetCount(thisVector))返回否;
对于(CFIndex bitIndex=0;bitIndex
根据您有多少位,为什么不调用CfitVectorGetBits
并将它们转储到一个无符号的int
或long
中,然后比较是否相等?请删除“C”标记。这与C无关。@r-d代码是C,我想答案也是。所以,我被你的评论搞糊涂了。@r-d把我之前的评论划掉。我忘记了vector对象是不透明的,无法直接访问底层位数组。所以,是的,在这种情况下不太像C-ish。@PaulGriffiths我可以有相当大的位向量,但我想我可以将它们分成32位或64位的部分,并比较积分值。不过,我不确定这是否会更有效率,我希望代码不会那么清晰。