Skip to content
BitVecInsertPattern

BitVecInsertPattern

Description

Insert a bit pattern from a byte at a specific position. Only the specified number of low-order bits from the pattern are inserted.

Parameters

Name Direction Description
bv in,out Bitvector to insert into.
idx in Position to insert at (0-based), in [0, length].
pattern in Byte containing the bit pattern.
pattern_bits in Number of low-order bits to take from pattern, 1-8.

Usage example (from documentation)

  u8 pattern = 0x0B; // 1011 in binary
  BitVecInsertPattern(&flags, 2, pattern, 4);  // Insert 1011 at position 2

Success

Returns true. Bitvector length grows by pattern_bits; the inserted bits at [idx, idx + pattern_bits) reflect the low pattern_bits of pattern (LSB-first); previous bits at and after idx have shifted right.

Failure

Returns false on allocation failure. The bitvector is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    bool BitVecInsertPattern(BitVec *bv, u64 idx, u8 pattern, u64 pattern_bits) {
        ValidateBitVec(bv);
        if (idx > bv->length) {
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecInsertPattern\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
        // Insert pattern 0x0B (1011 in binary) using 4 bits
        u8 pattern = 0x0B;
        BitVecInsertPattern(&bv, 1, pattern, 4);
    
        // Check result: false, true, false, true, true, false
    
        u8 pattern2 = 0x05;
        BitVecInsertPattern(&bv2, 0, pattern2, 3);
    
        // Check result: true, false, true, true (3 bits: 101)
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecInsertPattern edge cases\n");
    
        BitVec bv     = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Test inserting empty pattern (should be no-op)
        BitVecInsertPattern(&bv, 0, 0x00, 0);
        result = result && (BitVecLen(&bv) == 0);
    
        // Test inserting single bit pattern
        BitVecInsertPattern(&bv, 0, 0x01, 1); // 1 bit pattern
        result = result && (BitVecLen(&bv) == 1);
        // Test inserting 8-bit pattern
        BitVecClear(&bv);
        BitVecInsertPattern(&bv, 0, 0xAA, 8);            // 10101010 pattern
        result = result && (BitVecLen(&bv) == 8);
        result = result && (BitVecGet(&bv, 0) == false); // First bit of 0xAA
    
        // Test NULL bitvec - should abort
        BitVecInsertPattern(NULL, 0, 0xFF, 8);
    
        return false;
    #define BitVecMustInsertPattern(bv, idx, pattern, pattern_bits)                                                        \
        do {                                                                                                               \
            if (!BitVecInsertPattern((bv), (idx), (pattern), (pattern_bits))) {                                            \
                LOG_FATAL("BitVecMustInsertPattern failed");                                                               \
            }                                                                                                              \
Last updated on