BitVecSet

Table of Contents

BitVecSet

Description

Set the value of bit at given index in bitvector.

Parameters

NameDirectionDescription
bvinBitvector to set bit in
idxinIndex of bit to set (0-based)
valueinValue to set (true/false)

Usage example (from documentation)

  BitVecSet(&flags, 5, true);
  BitVecSet(&flags, 10, false);

Usage example (Cross-references)

    
    for (u64 i = bits; i > 0; i--) {
    BitVecSet(INT_BITS(value), i - 1 + positions, BitVecGet(INT_BITS(value), i - 1));
    }
    
    for (u64 i = 0; i < positions; i++) {
    BitVecSet(INT_BITS(value), i, false);
    }
    }
    
    for (u64 i = 0; i + positions < bits; i++) {
    BitVecSet(INT_BITS(value), i, BitVecGet(INT_BITS(value), i + positions));
    }
    BitVecResize(INT_BITS(&q), bit + 1);
    }
    BitVecSet(INT_BITS(&q), bit, true);
    }
    for (u64 i = 0; i < bv->length; i++) {
    bool bit = BitVecGet(bv, i);
    BitVecSet(&clone, i, bit);
    }
    }
    
    void BitVecSet(BitVec *bitvec, u64 idx, bool value) {
    ValidateBitVec(bitvec);
    if (idx >= bitvec->length) {
    
    BitVecResize(bitvec, bitvec->length + 1);
    BitVecSet(bitvec, bitvec->length - 1, value);
    }
    for (u64 i = bitvec->length - 1; i > idx; i--) {
    bool bit = BitVecGet(bitvec, i - 1);
    BitVecSet(bitvec, i, bit);
    }
    }
    
    BitVecSet(bitvec, idx, value);
    }
    i--;
    bool bit = BitVecGet(bv, i);
    BitVecSet(bv, i + count, bit);
    }
    // Insert the new bits
    for (u64 i = 0; i < count; i++) {
    BitVecSet(bv, idx + i, value);
    }
    }
    i--;
    bool bit = BitVecGet(bv, i);
    BitVecSet(bv, i + other->length, bit);
    }
    for (u64 i = 0; i < other->length; i++) {
    bool bit = BitVecGet(other, i);
    BitVecSet(bv, idx + i, bit);
    }
    }
    i--;
    bool bit = BitVecGet(bv, i);
    BitVecSet(bv, i + pattern_bits, bit);
    }
    for (u64 i = 0; i < pattern_bits; i++) {
    bool bit = (pattern & (1u << i)) != 0;
    BitVecSet(bv, idx + i, bit);
    }
    }
    for (u64 i = idx; i < bv->length - 1; i++) {
    bool bit = BitVecGet(bv, i + 1);
    BitVecSet(bv, i, bit);
    }
    for (u64 i = idx + count; i < bv->length; i++) {
    bool bit = BitVecGet(bv, i);
    BitVecSet(bv, i - count, bit);
    }
    // Keep this bit
    if (write_idx != read_idx) {
    BitVecSet(bv, write_idx, bit);
    }
    write_idx++;
    bool bit_a = BitVecGet(a, i);
    bool bit_b = BitVecGet(b, i);
    BitVecSet(result, i, bit_a && bit_b);
    }
    }
    bool bit_a = i < a->length ? BitVecGet(a, i) : false;
    bool bit_b = i < b->length ? BitVecGet(b, i) : false;
    BitVecSet(result, i, bit_a || bit_b);
    }
    }
    bool bit_a = i < a->length ? BitVecGet(a, i) : false;
    bool bit_b = i < b->length ? BitVecGet(b, i) : false;
    BitVecSet(result, i, bit_a != bit_b);
    }
    }
    for (u64 i = 0; i < bitvec->length; i++) {
    bool bit = BitVecGet(bitvec, i);
    BitVecSet(result, i, !bit);
    }
    }
    u64  bit_offset = i % 8;
    bool bit        = (bytes[byte_idx] & (1u << bit_offset)) != 0;
    BitVecSet(&result, i, bit);
    }
    for (u64 i = 0; i < bits; i++) {
    bool bit = (value & (1ULL << i)) != 0;
    BitVecSet(&result, i, bit);
    }
    for (u64 i = bv->length - 1; i >= positions; i--) {
    bool bit = BitVecGet(bv, i - positions);
    BitVecSet(bv, i, bit);
    }
    // Clear the leftmost bits (lower indices)
    for (u64 i = 0; i < positions; i++) {
    BitVecSet(bv, i, false);
    }
    }
    for (u64 i = 0; i < bv->length - positions; i++) {
    bool bit = BitVecGet(bv, i + positions);
    BitVecSet(bv, i, bit);
    }
    // Clear the high-index bits (rightmost bits that are now empty)
    for (u64 i = bv->length - positions; i < bv->length; i++) {
    BitVecSet(bv, i, false);
    }
    }
    u64  src_idx = (i + positions) % bv->length;
    bool bit     = BitVecGet(&temp, src_idx);
    BitVecSet(bv, i, bit);
    }
    u64  src_idx = (i + bv->length - positions) % bv->length;
    bool bit     = BitVecGet(&temp, src_idx);
    BitVecSet(bv, i, bit);
    }
    bool bit_i = BitVecGet(bv, i);
    bool bit_j = BitVecGet(bv, j);
    BitVecSet(bv, i, bit_j);
    BitVecSet(bv, j, bit_i);
    }
    bool bit_j = BitVecGet(bv, j);
    BitVecSet(bv, i, bit_j);
    BitVecSet(bv, j, bit_i);
    }
    }
    // Test BitVecSet function
    bool test_bitvec_set(void) {
    WriteFmt("Testing BitVecSet\n");
    
    BitVec bv = BitVecInit();
    // Reserve space and set bits
    BitVecResize(&bv, 4);
    BitVecSet(&bv, 0, true);
    BitVecSet(&bv, 1, false);
    BitVecSet(&bv, 2, true);
    BitVecResize(&bv, 4);
    BitVecSet(&bv, 0, true);
    BitVecSet(&bv, 1, false);
    BitVecSet(&bv, 2, true);
    BitVecSet(&bv, 3, false);
    BitVecSet(&bv, 0, true);
    BitVecSet(&bv, 1, false);
    BitVecSet(&bv, 2, true);
    BitVecSet(&bv, 3, false);
    BitVecSet(&bv, 1, false);
    BitVecSet(&bv, 2, true);
    BitVecSet(&bv, 3, false);
    
    // Test getting the set bits
    // Edge case tests for BitVecSet
    bool test_bitvec_set_edge_cases(void) {
    WriteFmt("Testing BitVecSet edge cases\n");
    
    BitVec bv = BitVecInit();
    // Set first bit
    BitVecResize(&bv, 1);
    BitVecSet(&bv, 0, true);
    bool result = (BitVecGet(&bv, 0) == true);
    
    // Set same bit to false
    BitVecSet(&bv, 0, false);
    result = result && (BitVecGet(&bv, 0) == false);
    BitVecPush(&bv, true);
    BitVecResize(&bv, 5);
    BitVecSet(&bv, 1, false);
    BitVecSet(&bv, 2, true);
    BitVecSet(&bv, 3, false);
    BitVecResize(&bv, 5);
    BitVecSet(&bv, 1, false);
    BitVecSet(&bv, 2, true);
    BitVecSet(&bv, 3, false);
    BitVecSet(&bv, 4, true);
    BitVecSet(&bv, 1, false);
    BitVecSet(&bv, 2, true);
    BitVecSet(&bv, 3, false);
    BitVecSet(&bv, 4, true);
    BitVecSet(&bv, 2, true);
    BitVecSet(&bv, 3, false);
    BitVecSet(&bv, 4, true);
    
    // Verify pattern: T F T F T
    
    // Test some bit operations
    BitVecSet(&bv, 0, false);
    BitVecSet(&bv, 1, true);
    // Test some bit operations
    BitVecSet(&bv, 0, false);
    BitVecSet(&bv, 1, true);
    
    result = result && (BitVecGet(&bv, 0) == false);
    for (int i = 0; i < size; i++) {
    BitVecResize(&bv, i + 1);
    BitVecSet(&bv, i, i % 3 == 0); // Every third bit is true
    }
    
    // Change one bit to false
    BitVecSet(&bv, 500, false);
    result = result && !BitVecAll(&bv, true);
    result = result && BitVecAny(&bv, true);
    
    // Test with one interruption in the middle
    BitVecSet(&bv, 5000, false);
    result = result && (BitVecLongestRun(&bv, true) == 5000);
    result = result && (BitVecLongestRun(&bv, false) == 1);
    
    // Test NULL bitvec pointer - should abort
    BitVecSet(NULL, 0, true);
    
    return false;
    
    // Test set on empty bitvec - should abort
    BitVecSet(&bv, 0, true);
    
    BitVecDeinit(&bv);
    
    // Test with index way beyond length (2) - should abort
    BitVecSet(&bv, 500, true);
    
    BitVecDeinit(&bv);
    
    // 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 non-superset case
    BitVecSet(&superset, 2, false); // Change to 1101
    // Now superset (1101) is not a superset of subset (1010)
    result = result && !BitVecIsSuperset(&superset, &subset);
    
    // Create intersecting bitvectors
    BitVecSet(&bv2, 0, true); // Change bv2 to 1101
    
    // Should not be disjoint and should intersect
    
    // Modify clone to verify independence
    BitVecSet(&clone, 0, false);
    
    // Original should remain unchanged at index 0
    
    // Test independence - modify original
    BitVecSet(&bv, 0, !BitVecGet(&bv, 0));
    result = result && (BitVecGet(&clone3, 0) != BitVecGet(&bv, 0));
    // Test BitVecSet function
    bool test_bitvec_set(void) {
    WriteFmt("Testing BitVecSet\n");
    
    BitVec bv = BitVecInit();
    
    // Set specific bits
    BitVecSet(&bv, 0, true);
    BitVecSet(&bv, 2, true);
    // Set specific bits
    BitVecSet(&bv, 0, true);
    BitVecSet(&bv, 2, true);
    
    // Verify changes
    
    // Test setting back to false
    BitVecSet(&bv, 0, false);
    result = result && (BitVecGet(&bv, 0) == false);
    
    bool test_bitvec_set_edge_cases(void) {
    WriteFmt("Testing BitVecSet edge cases\n");
    
    BitVec bv     = BitVecInit();
    // Test normal setting
    BitVecPush(&bv, false);
    BitVecSet(&bv, 0, true);
    result = result && (BitVecGet(&bv, 0) == true);
    
    // Test setting same value multiple times
    BitVecSet(&bv, 0, true);
    BitVecSet(&bv, 0, true);
    result = result && (BitVecGet(&bv, 0) == true);
    // Test setting same value multiple times
    BitVecSet(&bv, 0, true);
    BitVecSet(&bv, 0, true);
    result = result && (BitVecGet(&bv, 0) == true);
    
    // Test setting and getting
    BitVecSet(&bv, cycle, cycle % 3 == 0);
    result = result && (BitVecGet(&bv, cycle) == (cycle % 3 == 0));
    }
    // Change to all-ones pattern
    for (int i = 0; i < 100; i++) {
    BitVecSet(&bv, i, true);
    }
    
    // Test NULL bitvec pointer - should abort
    BitVecSet(NULL, 0, true);
    
    return false;
    
    // Test set on empty bitvec - should abort
    BitVecSet(&bv, 0, true);
    
    BitVecDeinit(&bv);
    
    // Test with index way beyond length (2) - should abort
    BitVecSet(&bv, 500, true);
    
    BitVecDeinit(&bv);
    
    // Change one bit to false
    BitVecSet(&bv, 500, false);
    result = result && !BitVecAll(&bv, true);
    result = result && BitVecAny(&bv, true);
    
    // Test with one interruption in the middle
    BitVecSet(&bv, 5000, false);
    result = result && (BitVecLongestRun(&bv, true) == 5000);
    result = result && (BitVecLongestRun(&bv, false) == 1);

Share :

Related Posts

BitVecSwap

BitVecSwap Description Efficiently swap the contents of two bitvectors. This is much faster than copying both bitvectors.

Read More

BitVecClone

BitVecClone Description Create a deep copy of a bitvector. The returned bitvector must be deinitialized when no longer needed.

Read More

BitVecShrinkToFit

BitVecShrinkToFit Description Reduce bitvector capacity to match its current length. Frees any unused memory allocated beyond the current length.

Read More