BitVecWeightCompare
Description
Compare two bitvectors by their Hamming weights (number of 1s).
Parameters
| Name | Direction | Description |
|---|---|---|
bv1 |
in | First bitvector |
bv2 |
in | Second bitvector |
Usage example (from documentation)
int result = BitVecWeightCompare(&flags1, &flags2);Success
-1 if bv1 has fewer 1s, 0 if equal, 1 if bv1 has more 1s
Failure
Cannot fail; aborts on a corrupted magic via the validator.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
BitVec.c:725:
}
int BitVecWeightCompare(const BitVec *bv1, const BitVec *bv2) {
ValidateBitVec(bv1);
ValidateBitVec(bv2); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecWeightCompare\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
// Weight comparison should compare number of set bits
int cmp_result = BitVecWeightCompare(&bv1, &bv2);
bool result = (cmp_result > 0); // 3 ones > 2 ones
BitVecPush(&bv2, true); // Also 3 ones
result = result && (BitVecWeightCompare(&bv1, &bv2) == 0);
// Clean up
// Weight: bv1 has 4 ones, bv2 has 4 ones, so equal weight
result = result && (BitVecWeightCompare(&bv1, &bv2) == 0);
// Lexicographic comparison
Last updated on