BitVecFromStr

Table of Contents

BitVecFromStr

Description

Create bitvector from string representation. String should contain only ‘1’ and ‘0’ characters.

Parameters

NameDirectionDescription
strinString containing bit pattern like “10110”

Usage example (from documentation)

  BitVec flags = BitVecFromString("10110");
  // ... use bitvector ...
  BitVecDeinit(&flags);

Usage example (Cross-references)

    
    // Convert to BitVec using the null-terminated string
    *bv = BitVecFromStr(bin_str.data);
    
    StrDeinit(&bin_str);
    }
    
    BitVec BitVecFromStr(const char *str) {
    if (!str) {
    LOG_FATAL("str is NULL");
    
    // Test 1: Basic binary formatting
    BitVec bv1 = BitVecFromStr("10110");
    StrWriteFmt(&output, "{}", bv1);
    success = success && (ZstrCompare(output.data, "10110") == 0);
    // Test BitVecFromStr function
    bool test_bitvec_from_string(void) {
    WriteFmt("Testing BitVecFromStr\n");
    
    // Convert from string
    // Convert from string
    const char *str = "1011";
    BitVec      bv  = BitVecFromStr(str);
    
    // Check result
    
    // Test with empty string
    BitVec empty_bv = BitVecFromStr("");
    result          = result && (empty_bv.length == 0);
    
    bool test_bitvec_from_string_edge_cases(void) {
    WriteFmt("Testing BitVecFromStr edge cases\n");
    
    bool result = true;
    
    // Test empty string
    BitVec bv1 = BitVecFromStr("");
    result     = result && (bv1.length == 0);
    BitVecDeinit(&bv1);
    
    // Test single character
    BitVec bv2 = BitVecFromStr("1");
    result     = result && (bv2.length == 1);
    result     = result && (BitVecGet(&bv2, 0) == true);
    long_str[1000] = '\0';
    
    BitVec bv3 = BitVecFromStr(long_str);
    result     = result && (bv3.length == 1000);
    result     = result && (BitVecGet(&bv3, 0) == true);
    
    for (size_t i = 0; i < sizeof(patterns) / sizeof(patterns[0]); i++) {
    BitVec bv  = BitVecFromStr(patterns[i]);
    Str    str = BitVecToStr(&bv);
    
    for (size_t i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
    BitVec bv = BitVecFromStr(test_cases[i].pattern);
    
    // Test string conversion consistency
    
    // Test cross-format validation
    BitVec bv1 = BitVecFromStr("11010110");
    BitVec bv2 = BitVecFromInteger(0xD6, 8); // Assuming MSB-first: 11010110 = 0xD6
    BitVec bv3 = BitVecFromBytes((u8[]) {0xD6}, 8);
    large_pattern[2000] = '\0';
    
    BitVec large_from_str = BitVecFromStr(large_pattern);
    result                = result && (large_from_str.length == 2000);
    
    // Test NULL string - should abort
    BitVecFromStr(NULL);
    
    return false;

Share :

Related Posts

BitVecAlignmentScore

BitVecAlignmentScore Description Calculate alignment score between two bitvectors. Used in bioinformatics-style sequence alignment with match/mismatch scoring.

Read More

BitVecCorrelation

BitVecCorrelation Description Calculate Pearson correlation coefficient between two bitvectors. Treats bits as 0/1 values and computes linear correlation.

Read More

BitVecDotProduct

BitVecDotProduct Description Calculate dot product of two bitvectors. Dot product is the count of positions where both bits are 1.

Read More