BitVecShiftRight
Description
Shift all bits in bitvector to the right by specified positions. New bits on the left (high end) are filled with zeros.
Parameters
| Name | Direction | Description |
|---|---|---|
bv |
in,out | Bitvector to shift in place. |
positions |
in | Number of positions to shift right. |
Usage example (from documentation)
BitVecShiftRight(&flags, 2);Success
Returns to the caller. Each previous bit at index i is now at index i - positions (when non-negative); bits at indices >= length - positions are now false; bits that shifted past 0 are discarded. Length and capacity are unchanged.
Failure
Function cannot fail. An invalid bitvector is a caller bug and aborts via LOG_FATAL.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
BitVec.c:1087:
}
void BitVecShiftRight(BitVec *bv, u64 positions) {
ValidateBitVec(bv);
if (positions == 0 || bv->length == 0) { DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecShiftRight\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc)); // New bit[3] = 0 (filled)
// Expected result: 1100
BitVecShiftRight(&bv, 2);
bool test_result = (BitVecLen(&bv) == 4); result = result && (BitVecLen(&bv) == 0);
BitVecShiftRight(&bv, 3);
result = result && (BitVecLen(&bv) == 0); // Shift left by 1, then right by 1 - should NOT restore original (data loss)
BitVecShiftLeft(&bv, 1);
BitVecShiftRight(&bv, 1);
// Should be different from original (lost MSB, gained LSB zero)
}
BitVecShiftRight(&bv, 10);
result = result && (BitVecLen(&bv) == 0);
// Test NULL pointer for shift right - should abort
BitVecShiftRight(NULL, 5);
return false; DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmtLn("Testing BitVecShiftRight");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc)); // New bit[3] = 0 (filled)
// Expected result: 1100
BitVecShiftRight(&bv, 2);
bool test_result = (BitVecLen(&bv) == 4); result = result && (BitVecLen(&bv) == 0);
BitVecShiftRight(&bv, 3);
result = result && (BitVecLen(&bv) == 0); // Shift left by 1, then right by 1 - should NOT restore original (data loss)
BitVecShiftLeft(&bv, 1);
BitVecShiftRight(&bv, 1);
// Should be different from original (lost MSB, gained LSB zero)
}
BitVecShiftRight(&bv, 10);
result = result && (BitVecLen(&bv) == 0);
// Test NULL pointer for shift right - should abort
BitVecShiftRight(NULL, 5);
return false;
Last updated on