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);
    // 424:5 remove_void_call -- BitVecRemove must validate its bitvec argument.
    bool test_remove_null_aborts(void) {
        WriteFmt("Testing BitVecRemove NULL validation\n");
    
        BitVecRemove(NULL, 0);
        WriteFmt("Testing BitVecRemove NULL validation\n");
    
        BitVecRemove(NULL, 0);
    
        // If we reach here, validation did not abort.
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecRemove rejects idx == length\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // length is 3; idx 3 is out of range and must abort.
        BitVecRemove(&bv, 3);
    
        // If we reach here, validation did not abort.
Last updated on