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

BitVecShiftRight

BitVecShiftRight Description Shift all bits in bitvector to the right by specified positions. New bits on the left are filled with zeros.

Read More

BitVecRotateLeft

BitVecRotateLeft Description Rotate all bits in bitvector to the left by specified positions. Bits that fall off the left end wrap around to the right.

Read More

BitVecRotateRight

BitVecRotateRight Description Rotate all bits in bitvector to the right by specified positions. Bits that fall off the right end wrap around to the left.

Read More