BitVecIsSubset

Table of Contents

BitVecIsSubset

Description

Check if first bitvector is a subset of the second. A bitvector is a subset if all its 1-bits are also 1-bits in the other.

Parameters

NameDirectionDescription
bv1inPotential subset bitvector
bv2inPotential superset bitvector

Usage example (from documentation)

  bool is_subset = BitVecIsSubset(&small_set, &large_set);

Usage example (Cross-references)

    }
    
    bool BitVecIsSubset(BitVec *bv1, BitVec *bv2) {
    ValidateBitVec(bv1);
    ValidateBitVec(bv2);
    
    bool BitVecIsSuperset(BitVec *bv1, BitVec *bv2) {
    return BitVecIsSubset(bv2, bv1);
    }
    // Test BitVecIsSubset function
    bool test_bitvec_is_subset(void) {
    WriteFmt("Testing BitVecIsSubset\n");
    
    BitVec subset   = BitVecInit();
    
    // subset should be a subset of superset (all 1s in subset are also 1s in superset)
    bool result = BitVecIsSubset(&subset, &superset);
    
    // Test non-subset case
    BitVecSet(&superset, 2, false); // Change superset to 1101
    // Now subset (1010) is not a subset of superset (1101) because subset has 1 at position 2
    result = result && !BitVecIsSubset(&subset, &superset);
    
    // Test equal sets (should be subset)
    BitVecPush(&superset, false);
    
    result = result && BitVecIsSubset(&subset, &superset);
    
    // Clean up
    BitVecClear(&bv1);
    BitVecClear(&bv2);
    result = result && BitVecIsSubset(&bv1, &bv2); // Empty is subset of empty
    
    BitVecDeinit(&bv1);
    BitVecPush(&superset, true);
    
    result = result && BitVecIsSubset(&subset, &superset);
    result = result && BitVecIsSuperset(&superset, &subset);
    
    // Test NULL pointer - should abort
    BitVecIsSubset(NULL, NULL);
    
    return false;

Share :

Related Posts

BitVecRemove

BitVecRemove Description Remove a bit at given index from bitvector. Shifts all bits after the index to the left.

Read More

BitVecCompareRange

BitVecCompareRange Description Compare ranges of two bitvectors lexicographically.

Read More

BitVecPop

BitVecPop Description Pop the last bit from bitvector. Returns the value of the removed bit.

Read More