Skip to content

BitVecInsert

Description

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

Parameters

Name Direction Description
bv in,out Bitvector to insert bit into.
idx in Index at which to insert bit (0-based), in [0, length].
value in Bit value to insert (true/false).

Usage example (from documentation)

  BitVecInsert(&flags, 5, true);

Success

Returns true. Bitvector length grows by one; bit at index idx is now value; previous bits at and after idx have shifted right by one.

Failure

Returns false on allocation failure. The bitvector is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    #define BitVecMustInsert(bv, idx, value)                                                                               \
        do {                                                                                                               \
            if (!BitVecInsert((bv), (idx), (value))) {                                                                     \
                LOG_FATAL("BitVecMustInsert failed");                                                                      \
            }                                                                                                              \
    }
    
    bool BitVecInsert(BitVec *bitvec, u64 idx, bool value) {
        ValidateBitVec(bitvec);
        if (idx > bitvec->length) {
    
        for (u64 i = 0; i < new_pattern->length; i++) {
            if (!BitVecInsert(bv, pos + i, BitVecGet(new_pattern, i))) {
                return false;
            }
    
            for (u64 i = 0; i < new_pattern->length; i++) {
                if (!BitVecInsert(bv, match_pos + i, BitVecGet(new_pattern, i))) {
                    return replacements;
                }
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecInsert (single bit)\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Insert at index 0 (empty bitvector)
        BitVecInsert(&bv, 0, true);
    
        // Check first bit
    
        // Insert at the end
        BitVecInsert(&bv, 1, false);
    
        // Check bits
    
        // Insert in the middle
        BitVecInsert(&bv, 1, true);
    
        // Check all bits
    // With validation removed, a NULL bitvec is accepted instead of aborting.
    bool test_insert_null_aborts(void) {
        WriteFmt("Testing BitVecInsert NULL validation\n");
    
        BitVecInsert(NULL, 0, true);
        WriteFmt("Testing BitVecInsert NULL validation\n");
    
        BitVecInsert(NULL, 0, true);
    
        // If we reach here, validation did not abort.
Last updated on