Skip to content

BitVecSet

Description

Set the value of bit at given index in bitvector.

Parameters

Name Direction Description
bv in,out Bitvector to set bit in.
idx in Index of bit to set (0-based), in [0, length).
value in Value to set (true/false).

Usage example (from documentation)

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

Success

Returns to the caller. The bit at idx is now value. Length and capacity are unchanged; all other bits are unchanged.

Failure

Function cannot fail. An out-of-range idx is a caller bug and aborts via LOG_FATAL.

Usage example (Cross-references)

Usage examples (Cross-references)
        for (u64 i = 0; i < bv->length; i++) {
            bool bit = BitVecGet(bv, i);
            BitVecSet(out, i, bit);
        }
    }
    
    void BitVecSet(BitVec *bitvec, u64 idx, bool value) {
        ValidateBitVec(bitvec);
        if (idx >= bitvec->length) {
            return false;
        }
        BitVecSet(bitvec, bitvec->length - 1, value);
        return true;
    }
        for (u64 i = bitvec->length - 1; i > idx; i--) {
            bool bit = BitVecGet(bitvec, i - 1);
            BitVecSet(bitvec, i, bit);
        }
        }
    
        BitVecSet(bitvec, idx, value);
        return true;
    }
            i--;
            bool bit = BitVecGet(bv, i);
            BitVecSet(bv, i + count, bit);
        }
    
        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);
        }
            if (bit != value) {
                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(out, i, bit);
        }
        for (u64 i = 0; i < bits; i++) {
            bool bit = (value & (1ULL << i)) != 0;
            BitVecSet(out, i, bit);
        }
        for (u64 i = bv->length - 1; i >= positions; i--) {
            bool bit = BitVecGet(bv, i - positions);
            BitVecSet(bv, i, bit);
        }
    
        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);
        }
    
        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);
        }
    }
    
        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));
        }
                        goto cleanup;
                    }
                    BitVecSet(INT_BITS(&q), bit, true);
                }
    
        // 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 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);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecSet\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
        // 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
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecSet edge cases\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
        // 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 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
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecSet\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // 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);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecSet edge cases\n");
    
        BitVec bv     = BitVecInit(ALLOCATOR_OF(&alloc));
        // 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);
Last updated on