BitVecFlip
Description
Flip the value of bit at given index in bitvector. Changes 0 to 1 and 1 to 0.
Parameters
| Name | Direction | Description |
|---|---|---|
bv |
in,out | Bitvector to flip bit in. |
idx |
in | Index of bit to flip (0-based), in [0, length). |
Usage example (from documentation)
BitVecFlip(&flags, 5);Success
Returns to the caller. The bit at idx is now the logical inverse of its previous value. Length, capacity, and 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)
- In
BitVec.c:282:
}
void BitVecFlip(BitVec *bitvec, u64 idx) {
ValidateBitVec(bitvec);
if (idx >= bitvec->length) {
// Test NULL bitvec pointer - should abort
BitVecFlip(NULL, 0);
return false;
// Test flip on empty bitvec - should abort
BitVecFlip(&bv, 0);
BitVecDeinit(&bv);
// Test with index exactly at length (invalid) - should abort
BitVecFlip(&bv, 10);
BitVecDeinit(&bv); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecFlip\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
// Flip some bits
BitVecFlip(&bv, 0);
BitVecFlip(&bv, 1); // Flip some bits
BitVecFlip(&bv, 0);
BitVecFlip(&bv, 1);
// Test the flipped bits
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecFlip edge cases\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc)); // Test flipping single bit
BitVecPush(&bv, false);
BitVecFlip(&bv, 0);
bool result = (BitVecGet(&bv, 0) == true);
// Flip it again
BitVecFlip(&bv, 0);
result = result && (BitVecGet(&bv, 0) == false);
// Flip some bits and verify
BitVecFlip(&bv, 1); // F -> T
BitVecFlip(&bv, 3); // F -> T
// Flip some bits and verify
BitVecFlip(&bv, 1); // F -> T
BitVecFlip(&bv, 3); // F -> T
result = result && (BitVecCountOnes(&bv) == 5);
// Flip some bits and verify
BitVecFlip(&bv, 0); // T -> F
BitVecFlip(&bv, 1); // F -> T
BitVecFlip(&bv, 500); // T -> F
// Flip some bits and verify
BitVecFlip(&bv, 0); // T -> F
BitVecFlip(&bv, 1); // F -> T
BitVecFlip(&bv, 500); // T -> F
BitVecFlip(&bv, 999); // F -> T
BitVecFlip(&bv, 0); // T -> F
BitVecFlip(&bv, 1); // F -> T
BitVecFlip(&bv, 500); // T -> F
BitVecFlip(&bv, 999); // F -> T
BitVecFlip(&bv, 1); // F -> T
BitVecFlip(&bv, 500); // T -> F
BitVecFlip(&bv, 999); // F -> T
result = result && (BitVecCountOnes(&bv) == 500); // Flip every 7th bit
for (int i = 0; i < size; i += 7) {
BitVecFlip(&bv, i);
} DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecFlip\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
// Flip each bit
BitVecFlip(&bv, 0); // 1 -> 0
BitVecFlip(&bv, 1); // 0 -> 1
BitVecFlip(&bv, 2); // 1 -> 0
// Flip each bit
BitVecFlip(&bv, 0); // 1 -> 0
BitVecFlip(&bv, 1); // 0 -> 1
BitVecFlip(&bv, 2); // 1 -> 0
BitVecFlip(&bv, 0); // 1 -> 0
BitVecFlip(&bv, 1); // 0 -> 1
BitVecFlip(&bv, 2); // 1 -> 0
// Verify flipped values: should now be 010
// Flip back
BitVecFlip(&bv, 0); // 0 -> 1
BitVecFlip(&bv, 1); // 1 -> 0
BitVecFlip(&bv, 2); // 0 -> 1
// Flip back
BitVecFlip(&bv, 0); // 0 -> 1
BitVecFlip(&bv, 1); // 1 -> 0
BitVecFlip(&bv, 2); // 0 -> 1
BitVecFlip(&bv, 0); // 0 -> 1
BitVecFlip(&bv, 1); // 1 -> 0
BitVecFlip(&bv, 2); // 0 -> 1
// Should be back to original: 101
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecFlip edge cases\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc)); // Test normal flipping
BitVecPush(&bv, true);
BitVecFlip(&bv, 0);
result = result && (BitVecGet(&bv, 0) == false); result = result && (BitVecGet(&bv, 0) == false);
BitVecFlip(&bv, 0);
result = result && (BitVecGet(&bv, 0) == true); if (boundaries[i] < size) {
bool original = BitVecGet(&bv, boundaries[i]);
BitVecFlip(&bv, boundaries[i]);
result = result && (BitVecGet(&bv, boundaries[i]) == !original);
BitVecFlip(&bv, boundaries[i]); // Flip back
BitVecFlip(&bv, boundaries[i]);
result = result && (BitVecGet(&bv, boundaries[i]) == !original);
BitVecFlip(&bv, boundaries[i]); // Flip back
result = result && (BitVecGet(&bv, boundaries[i]) == original);
}
// Test NULL bitvec pointer - should abort
BitVecFlip(NULL, 0);
return false;
// Test flip on empty bitvec - should abort
BitVecFlip(&bv, 0);
BitVecDeinit(&bv);
// Test with index exactly at length (invalid) - should abort
BitVecFlip(&bv, 10);
BitVecDeinit(&bv);
Last updated on