Skip to content

BitVecRemove

Description

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

Parameters

Name Direction Description
bv in,out Bitvector to remove bit from.
idx in Index of bit to remove (0-based), in [0, length).

Usage example (from documentation)

  bool removed_bit = BitVecRemove(&flags, 5);

Success

Returns the value of the removed bit (true/false). Bitvector length shrinks by one; bits previously at indices > idx have shifted left by one.

Failure

Function cannot fail. An out-of-range idx is a caller bug and aborts via LOG_FATAL.

Usage example (Cross-references)

Usage examples (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;
        }
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecRemove (single bit)\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // 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) && (BitVecLen(&bv) == 3);
        result  = result && (BitVecGet(&bv, 0) == false);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecRemove edge cases\n");
    
        BitVec bv     = BitVecInit(ALLOCATOR_OF(&alloc));
        // Test remove last element
        BitVecPush(&bv, true);
        bool removed = BitVecRemove(&bv, 0);
        result       = result && (removed == true) && (BitVecLen(&bv) == 0);
    
        // Remove middle element
        removed = BitVecRemove(&bv, 500);
        result  = result && (removed == (500 % 3 == 0)); // Should return the value of the removed bit
        result  = result && (BitVecLen(&bv) == 999);
    
        // Test remove from empty bitvec - should abort
        BitVecRemove(&bv, 0);
    
        BitVecDeinit(&bv);
Last updated on