BitVecLen

Table of Contents

BitVecLen

Description

Get number of bits currently in bitvector.

Parameters

NameDirectionDescription
bvinBitvector to get length of

Usage example (from documentation)

  u64 num_bits = BitVecLen(&flags);

Usage example (Cross-references)

    /// TAGS: BitVec, Empty, Check
    ///
    #define BitVecEmpty(bv) (BitVecLen(bv) == 0)
    
    ///
    
    // Initially empty
    bool result = (BitVecLen(&bv) == 0);
    
    // Push some bits
    BitVecPush(&bv, true);
    
    result = result && (BitVecLen(&bv) == 3);
    result = result && (BitVecCapacity(&bv) >= 3);
    // Reserve more space
    BitVecReserve(&bv, 100);
    result = result && (BitVecLen(&bv) == 3);
    result = result && (BitVecCapacity(&bv) >= 100);
    
    // Test length and capacity macros if they exist
    result = result && (BitVecLen(&bv) == 2);
    result = result && (BitVecCapacity(&bv) >= 2);
    
    // Check result should be: 101110
    result = result && (BitVecLen(&source) == 6);
    result = result && (BitVecGet(&source, 0) == true);
    result = result && (BitVecGet(&source, 1) == false);
    
    // Check final length
    result = result && (BitVecLen(&source) == 6); // 3 * 2 = 6
    
    BitVecDeinit(&source);
    
    // Check result should be: 101110
    result = result && (BitVecLen(&source) == 6);
    result = result && (BitVecGet(&source, 0) == true);
    result = result && (BitVecGet(&source, 1) == false);
    
    // Check final length
    result = result && (BitVecLen(&source) == 6); // 3 * 2 = 6
    
    BitVecDeinit(&source);
    
    // Initially should be empty
    bool result = (BitVecLen(&bv) == 0) && (BitVecCapacity(&bv) == 0);
    
    // Add some bits
    BitVecPush(&bv, true);
    
    result = result && (BitVecLen(&bv) == 3);
    result = result && (BitVecCapacity(&bv) >= 3);
    BitVecClear(&bv);
    result = result && BitVecEmpty(&bv);
    result = result && (BitVecLen(&bv) == 0);
    
    // Clean up
    if (cycle > 0) {
    result = result && (BitVecGet(&bv, 0) == true);
    result = result && (BitVecLen(&bv) == (size)(cycle + 1));
    }
    
    // Test all macros on empty bitvector
    result = result && (BitVecLen(&bv) == 0);
    result = result && (BitVecCapacity(&bv) == 0);
    result = result && BitVecEmpty(&bv);
    }
    
    result = result && (BitVecLen(&bv) == 65);
    result = result && (BitVecCapacity(&bv) >= 65);
    result = result && !BitVecEmpty(&bv);
    }
    
    result = result && (BitVecLen(&bv) == 64);
    result = result && (BitVecByteSize(&bv) >= 8); // At least 8 bytes for 64 bits

Share :

Related Posts

BitVecReserve

BitVecReserve Description Reserve space for at least n bits in bitvector. Does not change the length, only ensures capacity.

Read More

BitVecClear

BitVecClear Description Clear all bits in bitvector without deallocating memory. Sets length to 0 but keeps allocated capacity.

Read More

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