0

2 つのビット ベクトルを比較する最も効率的な方法は何ですか? Objective-C では、CFBitVectors を使用して、単純に両方の各ビットを比較しています。

for (CFIndex bitIndex = 0; bitIndex < numBits; bitIndex++) {
    if (CFBitVectorGetBitAtIndex(thisVector, bitIndex) != CFBitVectorGetBitAtIndex(thatVector, bitIndex)) {
        return NO;
    }
}
return YES;

これは問題なく機能しますが、ビット演算子を使用してより効率的な方法がないかどうかはわかりませんでした。

4

1 に答える 1

0

この回答で説明されているように、2 つの CFBitVectorRef 構造体に対してビットごとの OR 操作を実行できます。ただし、これは実装に依存するため、将来失敗する可能性があります。それらを比較する最も安全な方法は、OP で説明されているように、一度に 1 ビットずつのようです。すべてを行うユーティリティ関数を次に示します。

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;
}
于 2015-05-21T21:17:23.303 に答える