BitVecFromBytes

Table of Contents

BitVecFromBytes

Description

Create bitvector from byte array. Reads the specified number of bits from the byte array.

Parameters

NameDirectionDescription
bytesinByte array containing bit data
bit_leninNumber of bits to read from the byte array

Usage example (from documentation)

  u8 data[] = {0xFF, 0x00, 0xAA};
  BitVec flags = BitVecFromBytes(data, 20);  // Read 20 bits (needs 3 bytes)

Usage example (Cross-references)

    }
    
    Int result = int_wrap(BitVecFromBytes(bytes, len * 8));
    int_normalize(&result);
    return result;
    }
    
    BitVec BitVecFromBytes(const u8 *bytes, u64 bit_len) {
    if (!bytes) {
    LOG_FATAL("bytes is NULL");
    // Test BitVecFromBytes function
    bool test_bitvec_from_bytes(void) {
    WriteFmt("Testing BitVecFromBytes\n");
    
    // Create byte array
    // Create byte array
    u8     bytes[] = {0xB3};                    // 10110011 in binary
    BitVec bv      = BitVecFromBytes(bytes, 8); // 8 bits from the byte
    
    // Check result (8 bits from 1 byte)
    // Test bytes to bitvec with 0 bits (should return empty bitvector)
    u8     empty_bytes[1] = {0x05};
    BitVec bv2            = BitVecFromBytes(empty_bytes, 0); // 0 bits
    result                = result && (bv2.length == 0);
    BitVecDeinit(&bv2);
    // Test single byte
    u8     single_byte[1] = {0xFF};
    BitVec bv3            = BitVecFromBytes(single_byte, 8); // 8 bits from 1 byte
    result                = result && (bv3.length == 8);
    BitVecDeinit(&bv3);
    
    for (size_t i = 0; i < sizeof(test_bytes); i++) {
    BitVec bv             = BitVecFromBytes(&test_bytes[i], 8);
    u8     recovered_byte = 0;
    u64    written        = BitVecToBytes(&bv, &recovered_byte, 1);
    BitVec bv1 = BitVecFromStr("11010110");
    BitVec bv2 = BitVecFromInteger(0xD6, 8); // Assuming MSB-first: 11010110 = 0xD6
    BitVec bv3 = BitVecFromBytes((u8[]) {0xD6}, 8);
    
    // All three should produce the same result when converted back
    
    // Test round-trip from bytes
    BitVec recovered_bv = BitVecFromBytes(large_bytes, 1000);
    result              = result && (recovered_bv.length == 1000);
    // Test fromBytes with 0 bit length - should return empty bitvec
    u8     dummy_bytes[1] = {0xFF};
    BitVec empty_bv       = BitVecFromBytes(dummy_bytes, 0);
    bool   result         = (empty_bv.length == 0);
    BitVecDeinit(&empty_bv);
    
    // Test NULL bytes - should abort
    BitVecFromBytes(NULL, 8); // NULL bytes, 8 bits
    
    return false;

Share :

Related Posts

BitVecEditDistance

BitVecEditDistance Description Calculate edit distance between two bitvectors. Edit distance is minimum number of single-bit operations to transform one into the other.

Read More

BitVecToBytes

BitVecToBytes Description Export bitvector to byte array. Copies the internal bit representation to the provided byte array.

Read More

BitVecFromStr

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

Read More