Skip to content

BitVecCompare

Description

Compare two bitvectors lexicographically. Comparison is done bit by bit from left to right.

Parameters

Name Direction Description
bv1 in First bitvector
bv2 in Second bitvector

Usage example (from documentation)

  int result = BitVecCompare(&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)
    
    i32 bitvec_compare(const void *lhs, const void *rhs) {
        return (i32)BitVecCompare((const BitVec *)lhs, (const BitVec *)rhs);
    }
    }
    
    int BitVecCompare(const BitVec *bv1, const BitVec *bv2) {
        ValidateBitVec(bv1);
        ValidateBitVec(bv2);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecCompare\n");
    
        BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecPush(&bv2, false);
    
        bool result = (BitVecCompare(&bv1, &bv2) == 0);
    
        // Test first greater than second
        BitVecPush(&bv2, false); // 10
    
        result = result && (BitVecCompare(&bv1, &bv2) > 0);
        result = result && (BitVecCompare(&bv2, &bv1) < 0);
    
        result = result && (BitVecCompare(&bv1, &bv2) > 0);
        result = result && (BitVecCompare(&bv2, &bv1) < 0);
    
        // Clean up
    
        // Lexicographic comparison considers position-by-position
        int  cmp_result = BitVecCompare(&bv1, &bv2);
        bool result     = (cmp_result != 0); // Should not be equal
        BitVecPush(&bv2, true); // 101
    
        result = result && (BitVecCompare(&bv1, &bv2) == 0);
    
        // Clean up
    
        // Lexicographically, shorter comes first
        bool result = (BitVecCompare(&bv1, &bv2) < 0);
    
        // Numerically, 10 (2) < 101 (5)
        // Test equal cases
        BitVec bv3 = BitVecClone(&bv1);
        result     = result && !(BitVecCompare(&bv1, &bv3) < 0);
        result     = result && !(BitVecNumericalCompare(&bv1, &bv3) < 0);
    
        // Test reverse comparison
        result = result && !(BitVecCompare(&bv2, &bv1) < 0);
        result = result && !(BitVecNumericalCompare(&bv2, &bv1) < 0);
        // Test compare empty bitvecs
        result = result && BitVecEquals(&bv1, &bv2);
        result = result && (BitVecCompare(&bv1, &bv2) == 0);
    
        // Test compare empty vs non-empty
        BitVecPush(&bv1, true);
        result = result && !BitVecEquals(&bv1, &bv2);
        result = result && (BitVecCompare(&bv1, &bv2) != 0);
    
        // Test large identical bitvecs
        // Numerical: 86 < 89, so bv1 < bv2
        result = result && (BitVecNumericalCompare(&bv1, &bv2) < 0);
        result = result && (BitVecCompare(&bv1, &bv2) < 0);
    
        // Weight: bv1 has 4 ones, bv2 has 4 ones, so equal weight
    
        // Lexicographic comparison
        int  lex_cmp  = BitVecCompare(&bv1, &bv2);
        bool lex_less = (BitVecCompare(&bv1, &bv2) < 0);
        result        = result && ((lex_cmp < 0) == lex_less);
        // Lexicographic comparison
        int  lex_cmp  = BitVecCompare(&bv1, &bv2);
        bool lex_less = (BitVecCompare(&bv1, &bv2) < 0);
        result        = result && ((lex_cmp < 0) == lex_less);
Last updated on