Skip to content

BitVecFlip

BitVecFlip

Description

Flip the value of bit at given index in bitvector. Changes 0 to 1 and 1 to 0.

Parameters

Name Direction Description
bv in Bitvector to flip bit in
idx in Index of bit to flip (0-based)

Usage example (from documentation)

  BitVecFlip(&flags, 5);

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    void BitVecFlip(BitVec *bitvec, u64 idx) {
        ValidateBitVec(bitvec);
        if (idx >= bitvec->length) {
    // Test BitVecFlip function
    bool test_bitvec_flip(void) {
        WriteFmt("Testing BitVecFlip\n");
    
        BitVec bv = BitVecInit();
    
        // Flip some bits
        BitVecFlip(&bv, 0);
        BitVecFlip(&bv, 1);
        // Flip some bits
        BitVecFlip(&bv, 0);
        BitVecFlip(&bv, 1);
    
        // Test the flipped bits
    // Edge case tests for BitVecFlip
    bool test_bitvec_flip_edge_cases(void) {
        WriteFmt("Testing BitVecFlip edge cases\n");
    
        BitVec bv = BitVecInit();
        // Test flipping single bit
        BitVecPush(&bv, false);
        BitVecFlip(&bv, 0);
        bool result = (BitVecGet(&bv, 0) == true);
    
        // Flip it again
        BitVecFlip(&bv, 0);
        result = result && (BitVecGet(&bv, 0) == false);
    
        // Flip some bits and verify
        BitVecFlip(&bv, 1); // F -> T
        BitVecFlip(&bv, 3); // F -> T
        // Flip some bits and verify
        BitVecFlip(&bv, 1); // F -> T
        BitVecFlip(&bv, 3); // F -> T
    
        result = result && (BitVecCountOnes(&bv) == 5);
    
        // Flip some bits and verify
        BitVecFlip(&bv, 0);   // T -> F
        BitVecFlip(&bv, 1);   // F -> T
        BitVecFlip(&bv, 500); // T -> F
        // Flip some bits and verify
        BitVecFlip(&bv, 0);   // T -> F
        BitVecFlip(&bv, 1);   // F -> T
        BitVecFlip(&bv, 500); // T -> F
        BitVecFlip(&bv, 999); // F -> T
        BitVecFlip(&bv, 0);   // T -> F
        BitVecFlip(&bv, 1);   // F -> T
        BitVecFlip(&bv, 500); // T -> F
        BitVecFlip(&bv, 999); // F -> T
        BitVecFlip(&bv, 1);   // F -> T
        BitVecFlip(&bv, 500); // T -> F
        BitVecFlip(&bv, 999); // F -> T
    
        result = result && (BitVecCountOnes(&bv) == 500);
        // Flip every 7th bit
        for (int i = 0; i < size; i += 7) {
            BitVecFlip(&bv, i);
        }
    
        // Test NULL bitvec pointer - should abort
        BitVecFlip(NULL, 0);
    
        return false;
    
        // Test flip on empty bitvec - should abort
        BitVecFlip(&bv, 0);
    
        BitVecDeinit(&bv);
    
        // Test with index exactly at length (invalid) - should abort
        BitVecFlip(&bv, 10);
    
        BitVecDeinit(&bv);
    // Test BitVecFlip function
    bool test_bitvec_flip(void) {
        WriteFmt("Testing BitVecFlip\n");
    
        BitVec bv = BitVecInit();
    
        // Flip each bit
        BitVecFlip(&bv, 0); // 1 -> 0
        BitVecFlip(&bv, 1); // 0 -> 1
        BitVecFlip(&bv, 2); // 1 -> 0
        // Flip each bit
        BitVecFlip(&bv, 0); // 1 -> 0
        BitVecFlip(&bv, 1); // 0 -> 1
        BitVecFlip(&bv, 2); // 1 -> 0
        BitVecFlip(&bv, 0); // 1 -> 0
        BitVecFlip(&bv, 1); // 0 -> 1
        BitVecFlip(&bv, 2); // 1 -> 0
    
        // Verify flipped values: should now be 010
    
        // Flip back
        BitVecFlip(&bv, 0); // 0 -> 1
        BitVecFlip(&bv, 1); // 1 -> 0
        BitVecFlip(&bv, 2); // 0 -> 1
        // Flip back
        BitVecFlip(&bv, 0); // 0 -> 1
        BitVecFlip(&bv, 1); // 1 -> 0
        BitVecFlip(&bv, 2); // 0 -> 1
        BitVecFlip(&bv, 0); // 0 -> 1
        BitVecFlip(&bv, 1); // 1 -> 0
        BitVecFlip(&bv, 2); // 0 -> 1
    
        // Should be back to original: 101
    
    bool test_bitvec_flip_edge_cases(void) {
        WriteFmt("Testing BitVecFlip edge cases\n");
    
        BitVec bv     = BitVecInit();
        // Test normal flipping
        BitVecPush(&bv, true);
        BitVecFlip(&bv, 0);
        result = result && (BitVecGet(&bv, 0) == false);
        result = result && (BitVecGet(&bv, 0) == false);
    
        BitVecFlip(&bv, 0);
        result = result && (BitVecGet(&bv, 0) == true);
            if (boundaries[i] < size) {
                bool original = BitVecGet(&bv, boundaries[i]);
                BitVecFlip(&bv, boundaries[i]);
                result = result && (BitVecGet(&bv, boundaries[i]) == !original);
                BitVecFlip(&bv, boundaries[i]); // Flip back
                BitVecFlip(&bv, boundaries[i]);
                result = result && (BitVecGet(&bv, boundaries[i]) == !original);
                BitVecFlip(&bv, boundaries[i]); // Flip back
                result = result && (BitVecGet(&bv, boundaries[i]) == original);
            }
    
        // Test NULL bitvec pointer - should abort
        BitVecFlip(NULL, 0);
    
        return false;
    
        // Test flip on empty bitvec - should abort
        BitVecFlip(&bv, 0);
    
        BitVecDeinit(&bv);
    
        // Test with index exactly at length (invalid) - should abort
        BitVecFlip(&bv, 10);
    
        BitVecDeinit(&bv);
Last updated on