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) {
    printf("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) {
    printf("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

BitVecRunLengths

BitVecRunLengths Description Analyze run lengths in a bitvector. A run is a sequence of consecutive identical bits. Results array must be pre-allocated with sufficient space.

Read More

BitVecReplaceAll

BitVecReplaceAll Description Replace all occurrences of old pattern with new pattern.

Read More

BitVecSuffixMatch

BitVecSuffixMatch Description Match bitvector against an array of suffix patterns. Returns the index of the first matching suffix.

Read More