BitVecInsertPattern

Table of Contents

BitVecInsertPattern

Description

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

Parameters

NameDirectionDescription
bvinBitvector to insert into
idxinPosition to insert at (0-based)
patterninByte containing the bit pattern
pattern_bitsinNumber of 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

Usage example (Cross-references)

    }
    
    void BitVecInsertPattern(BitVec *bv, u64 idx, u8 pattern, u64 pattern_bits) {
    ValidateBitVec(bv);
    if (idx > bv->length) {
    // Test BitVecInsertPattern function
    bool test_bitvec_insert_pattern(void) {
    WriteFmt("Testing BitVecInsertPattern\n");
    
    BitVec bv = BitVecInit();
    // 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)
    
    bool test_bitvec_insert_pattern_edge_cases(void) {
    WriteFmt("Testing BitVecInsertPattern edge cases\n");
    
    BitVec bv     = BitVecInit();
    
    // Test inserting empty pattern (should be no-op)
    BitVecInsertPattern(&bv, 0, 0x00, 0);
    result = result && (bv.length == 0);
    
    // Test inserting single bit pattern
    BitVecInsertPattern(&bv, 0, 0x01, 1); // 1 bit pattern
    result = result && (bv.length == 1);
    // Test inserting 8-bit pattern
    BitVecClear(&bv);
    BitVecInsertPattern(&bv, 0, 0xAA, 8);            // 10101010 pattern
    result = result && (bv.length == 8);
    result = result && (BitVecGet(&bv, 0) == false); // First bit of 0xAA
    
    // Test NULL bitvec - should abort
    BitVecInsertPattern(NULL, 0, 0xFF, 8);
    
    return false;

Share :

Related Posts

BitVecInsertMultiple

BitVecInsertMultiple Description Insert all bits from another bitvector at a specific position. All existing bits at and after the position are shifted right.

Read More

BitVecInsertRange

BitVecInsertRange Description Insert multiple bits of the same value at a specific position. All existing bits at and after the position are shifted right.

Read More