Skip to content

BitVecAnd

Description

Perform bitwise AND operation between two bitvectors. Result is stored in result. Operand lengths must match.

Parameters

Name Direction Description
result out Bitvector to store result in. Pre-sized to the operand length by the helper if needed.
a in First bitvector operand.
b in Second bitvector operand.

Usage example (from documentation)

  BitVecAnd(&result, &flags1, &flags2);

Success

Returns to the caller. result->length == a->length; each bit result[i] == a[i] & b[i]. The operands a and b are unchanged.

Failure

Function cannot fail in an observable way - mismatched operand lengths or invalid bitvectors are caller bugs and abort via LOG_FATAL.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    void BitVecAnd(BitVec *result, BitVec *a, BitVec *b) {
        ValidateBitVec(result);
        ValidateBitVec(a);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecAnd\n");
    
        BitVec bv1    = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Perform AND operation
        BitVecAnd(&result, &bv1, &bv2);
    
        // Expected result: 1000 (1101 AND 1010)
        // Test operations on empty bitvecs
        BitVec result_bv = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecAnd(&result_bv, &bv1, &bv2);
        result = result && (BitVecLen(&result_bv) == 0);
        BitVecPush(&bv2, false);
    
        BitVecAnd(&result_bv, &bv1, &bv2);
        result = result && (BitVecLen(&result_bv) >= 1); // Should handle gracefully
    
        // Test AND with different lengths (result should be min length)
        BitVecAnd(&result, &bv1, &bv2);
        test_result = test_result && (BitVecLen(&result) == 4);
        BitVecPush(&bv2, false);
    
        BitVecAnd(&result, &bv1, &bv2);
        test_result = test_result && (BitVecLen(&result) == 1);
        test_result = test_result && (BitVecGet(&result, 0) == false);
    
        // Test A AND A = A
        BitVecAnd(&result, &bv1, &bv1);
        bool and_identity = true;
        for (int i = 0; i < 16; i++) {
        }
    
        BitVecAnd(&result, &bv1, &bv2);
        bool and_zero = true;
        for (int i = 0; i < 16; i++) {
    
        // Test A AND B = B AND A
        BitVecAnd(&result1, &bv1, &bv2);
        BitVecAnd(&result2, &bv2, &bv1);
        // Test A AND B = B AND A
        BitVecAnd(&result1, &bv1, &bv2);
        BitVecAnd(&result2, &bv2, &bv1);
    
        bool and_commutative = true;
    
        // Test AND on large data
        BitVecAnd(&result, &bv1, &bv2);
        test_result = test_result && (BitVecLen(&result) == 1000);
    
        // Test NULL pointer - should abort
        BitVecAnd(NULL, &bv, &bv2);
    
        BitVecDeinit(&bv);
    
        // Test NULL result pointer - should abort
        BitVecAnd(NULL, &bv1, &bv2);
    
        BitVecDeinit(&bv1);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmtLn("Testing BitVecAnd");
    
        BitVec bv1    = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Perform AND operation
        BitVecAnd(&result, &bv1, &bv2);
    
        // Expected result: 1000 (1101 AND 1010)
        // Test operations on empty bitvecs
        BitVec result_bv = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecAnd(&result_bv, &bv1, &bv2);
        result = result && (BitVecLen(&result_bv) == 0);
        BitVecPush(&bv2, false);
    
        BitVecAnd(&result_bv, &bv1, &bv2);
        result = result && (BitVecLen(&result_bv) >= 1); // Should handle gracefully
    
        // Test AND with different lengths (result should be min length)
        BitVecAnd(&result, &bv1, &bv2);
        test_result = test_result && (BitVecLen(&result) == 4);
        BitVecPush(&bv2, false);
    
        BitVecAnd(&result, &bv1, &bv2);
        test_result = test_result && (BitVecLen(&result) == 1);
        test_result = test_result && (BitVecGet(&result, 0) == false);
    
        // Test A AND A = A
        BitVecAnd(&result, &bv1, &bv1);
        bool and_identity = true;
        for (int i = 0; i < 16; i++) {
        }
    
        BitVecAnd(&result, &bv1, &bv2);
        bool and_zero = true;
        for (int i = 0; i < 16; i++) {
    
        // Test A AND B = B AND A
        BitVecAnd(&result1, &bv1, &bv2);
        BitVecAnd(&result2, &bv2, &bv1);
        // Test A AND B = B AND A
        BitVecAnd(&result1, &bv1, &bv2);
        BitVecAnd(&result2, &bv2, &bv1);
    
        bool and_commutative = true;
    
        // Test AND on large data
        BitVecAnd(&result, &bv1, &bv2);
        test_result = test_result && (BitVecLen(&result) == 1000);
    
        // Test NULL pointer - should abort
        BitVecAnd(NULL, &bv, &bv2);
    
        BitVecDeinit(&bv);
    
        // Test NULL result pointer - should abort
        BitVecAnd(NULL, &bv1, &bv2);
    
        BitVecDeinit(&bv1);
Last updated on