BitVecSet
- Function
- August 22, 2025
Table of Contents
BitVecSet
BitVecSet
Description
Set the value of bit at given index in bitvector.
Parameters
Name | Direction | Description |
---|---|---|
bv | in | Bitvector to set bit in |
idx | in | Index of bit to set (0-based) |
value | in | Value to set (true/false) |
Usage example (from documentation)
BitVecSet(&flags, 5, true);
BitVecSet(&flags, 10, false);
Usage example (Cross-references)
- In
BitVec.c:180
:
for (u64 i = 0; i < bv->length; i++) {
bool bit = BitVecGet(bv, i);
BitVecSet(&clone, i, bit);
}
- In
BitVec.c:197
:
}
void BitVecSet(BitVec *bitvec, u64 idx, bool value) {
ValidateBitVec(bitvec);
if (idx >= bitvec->length) {
- In
BitVec.c:231
:
BitVecResize(bitvec, bitvec->length + 1);
BitVecSet(bitvec, bitvec->length - 1, value);
}
- In
BitVec.c:255
:
for (u64 i = bitvec->length - 1; i > idx; i--) {
bool bit = BitVecGet(bitvec, i - 1);
BitVecSet(bitvec, i, bit);
}
- In
BitVec.c:258
:
}
BitVecSet(bitvec, idx, value);
}
- In
BitVec.c:278
:
i--;
bool bit = BitVecGet(bv, i);
BitVecSet(bv, i + count, bit);
}
- In
BitVec.c:283
:
// Insert the new bits
for (u64 i = 0; i < count; i++) {
BitVecSet(bv, idx + i, value);
}
}
- In
BitVec.c:305
:
i--;
bool bit = BitVecGet(bv, i);
BitVecSet(bv, i + other->length, bit);
}
- In
BitVec.c:311
:
for (u64 i = 0; i < other->length; i++) {
bool bit = BitVecGet(other, i);
BitVecSet(bv, idx + i, bit);
}
}
- In
BitVec.c:332
:
i--;
bool bit = BitVecGet(bv, i);
BitVecSet(bv, i + pattern_bits, bit);
}
- In
BitVec.c:338
:
for (u64 i = 0; i < pattern_bits; i++) {
bool bit = (pattern & (1u << i)) != 0;
BitVecSet(bv, idx + i, bit);
}
}
- In
BitVec.c:354
:
for (u64 i = idx; i < bv->length - 1; i++) {
bool bit = BitVecGet(bv, i + 1);
BitVecSet(bv, i, bit);
}
- In
BitVec.c:378
:
for (u64 i = idx + count; i < bv->length; i++) {
bool bit = BitVecGet(bv, i);
BitVecSet(bv, i - count, bit);
}
- In
BitVec.c:421
:
// Keep this bit
if (write_idx != read_idx) {
BitVecSet(bv, write_idx, bit);
}
write_idx++;
- In
BitVec.c:465
:
bool bit_a = BitVecGet(a, i);
bool bit_b = BitVecGet(b, i);
BitVecSet(result, i, bit_a && bit_b);
}
}
- In
BitVec.c:480
:
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);
}
}
- In
BitVec.c:495
:
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);
}
}
- In
BitVec.c:507
:
for (u64 i = 0; i < bitvec->length; i++) {
bool bit = BitVecGet(bitvec, i);
BitVecSet(result, i, !bit);
}
}
- In
BitVec.c:816
:
u64 bit_offset = i % 8;
bool bit = (bytes[byte_idx] & (1u << bit_offset)) != 0;
BitVecSet(&result, i, bit);
}
- In
BitVec.c:858
:
for (u64 i = 0; i < bits; i++) {
bool bit = (value & (1ULL << i)) != 0;
BitVecSet(&result, i, bit);
}
- In
BitVec.c:880
:
for (u64 i = bv->length - 1; i >= positions; i--) {
bool bit = BitVecGet(bv, i - positions);
BitVecSet(bv, i, bit);
}
- In
BitVec.c:885
:
// Clear the leftmost bits (lower indices)
for (u64 i = 0; i < positions; i++) {
BitVecSet(bv, i, false);
}
}
- In
BitVec.c:904
:
for (u64 i = 0; i < bv->length - positions; i++) {
bool bit = BitVecGet(bv, i + positions);
BitVecSet(bv, i, bit);
}
- In
BitVec.c:909
:
// 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);
}
}
- In
BitVec.c:931
:
u64 src_idx = (i + positions) % bv->length;
bool bit = BitVecGet(&temp, src_idx);
BitVecSet(bv, i, bit);
}
- In
BitVec.c:955
:
u64 src_idx = (i + bv->length - positions) % bv->length;
bool bit = BitVecGet(&temp, src_idx);
BitVecSet(bv, i, bit);
}
- In
BitVec.c:972
:
bool bit_i = BitVecGet(bv, i);
bool bit_j = BitVecGet(bv, j);
BitVecSet(bv, i, bit_j);
BitVecSet(bv, j, bit_i);
}
- In
BitVec.c:973
:
bool bit_j = BitVecGet(bv, j);
BitVecSet(bv, i, bit_j);
BitVecSet(bv, j, bit_i);
}
}
// 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) {
printf("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) {
printf("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 BitVecSet function
bool test_bitvec_set(void) {
printf("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) {
printf("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);
// 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