BitVecNumericalCompare
Description
Compare two bitvectors as unsigned integers. Treats bitvectors as unsigned binary numbers (LSB first).
Parameters
| Name | Direction | Description |
|---|---|---|
bv1 |
in | First bitvector |
bv2 |
in | Second bitvector |
Usage example (from documentation)
int result = BitVecNumericalCompare(&flags1, &flags2);Success
-1 if bv1 < bv2, 0 if equal, 1 if bv1 > bv2
Failure
Cannot fail; aborts on a corrupted magic via the validator.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
BitVec.c:702:
}
int BitVecNumericalCompare(const BitVec *bv1, const BitVec *bv2) {
ValidateBitVec(bv1);
ValidateBitVec(bv2);- In
BitVec.c:758:
}
int result = BitVecNumericalCompare(bv1, bv2);
// For two negatives, magnitude order is the opposite of value order
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecNumericalCompare\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
// Numerical comparison should compare the integer values
int cmp_result = BitVecNumericalCompare(&bv1, &bv2);
bool result = (cmp_result > 0); // 5 > 3
BitVecPush(&bv2, true); // Also 101
result = result && (BitVecNumericalCompare(&bv1, &bv2) == 0);
// Clean up
// Numerically, 10 (2) < 101 (5)
result = result && (BitVecNumericalCompare(&bv1, &bv2) < 0);
// Test equal cases
BitVec bv3 = BitVecClone(&bv1);
result = result && !(BitVecCompare(&bv1, &bv3) < 0);
result = result && !(BitVecNumericalCompare(&bv1, &bv3) < 0);
// Test reverse comparison
// Test reverse comparison
result = result && !(BitVecCompare(&bv2, &bv1) < 0);
result = result && !(BitVecNumericalCompare(&bv2, &bv1) < 0);
// Clean up
// Cross-validate different comparison methods
// Numerical: 86 < 89, so bv1 < bv2
result = result && (BitVecNumericalCompare(&bv1, &bv2) < 0);
result = result && (BitVecCompare(&bv1, &bv2) < 0); }
if (BitVecNumericalCompare(&bv1, &bv2) < 0 && BitVecNumericalCompare(&bv2, &bv3) < 0) {
result = result && (BitVecNumericalCompare(&bv1, &bv3) < 0);
}
if (BitVecNumericalCompare(&bv1, &bv2) < 0 && BitVecNumericalCompare(&bv2, &bv3) < 0) {
result = result && (BitVecNumericalCompare(&bv1, &bv3) < 0);
}
// Unsigned: 01111111 (127) < 10000001 (129)
result = result && (BitVecNumericalCompare(&pos, &neg) < 0);
// Signed: 01111111 (+127) > 10000001 (-127)
Last updated on