BitVecRemove

Table of Contents

BitVecRemove

Description

Remove a bit at given index from bitvector. Shifts all bits after the index to the left.

Parameters

NameDirectionDescription
bvinBitvector to remove bit from
idxinIndex of bit to remove (0-based)

Usage example (from documentation)

  bool removed_bit = BitVecRemove(&flags, 5);

Usage example (Cross-references)

    }
    
    bool BitVecRemove(BitVec *bv, u64 idx) {
    ValidateBitVec(bv);
    if (idx >= bv->length) {
    u64 pos = BitVecFind(bv, value);
    if (pos != SIZE_MAX) {
    BitVecRemove(bv, pos);
    return true;
    }
    u64 pos = BitVecFindLast(bv, value);
    if (pos != SIZE_MAX) {
    BitVecRemove(bv, pos);
    return true;
    }
    // Test BitVecRemove single bit function
    bool test_bitvec_remove_single(void) {
    WriteFmt("Testing BitVecRemove (single bit)\n");
    
    BitVec bv = BitVecInit();
    
    // Remove bit at index 2 (middle true)
    bool removed = BitVecRemove(&bv, 2);
    
    // Check result: true, false, false, true
    
    // Remove bit at index 0 (first bit)
    removed = BitVecRemove(&bv, 0);
    result  = result && (removed == true) && (bv.length == 3);
    result  = result && (BitVecGet(&bv, 0) == false);
    
    bool test_bitvec_remove_single_edge_cases(void) {
    WriteFmt("Testing BitVecRemove edge cases\n");
    
    BitVec bv     = BitVecInit();
    // Test remove last element
    BitVecPush(&bv, true);
    bool removed = BitVecRemove(&bv, 0);
    result       = result && (removed == true) && (bv.length == 0);
    
    // Remove middle element
    removed = BitVecRemove(&bv, 500);
    result  = result && (removed == (500 % 3 == 0)); // Should return the value of the removed bit
    result  = result && (bv.length == 999);
    
    // Test remove from empty bitvec - should abort
    BitVecRemove(&bv, 0);
    
    BitVecDeinit(&bv);

Share :

Related Posts

BitVecInsert

BitVecInsert Description Insert a bit at given index in bitvector. Shifts all bits at and after the index to the right.

Read More

BitVecRemoveRange

BitVecRemoveRange Description Remove multiple consecutive bits starting at a specific position. All bits after the removed range are shifted left.

Read More

BitVecPop

BitVecPop Description Pop the last bit from bitvector. Returns the value of the removed bit.

Read More