BitVecRotateRight
Description
Rotate all bits in bitvector to the right by specified positions. Bits that fall off the low end wrap around to the high end.
Parameters
| Name | Direction | Description |
|---|---|---|
bv |
in,out | Bitvector to rotate in place. |
positions |
in | Number of positions to rotate right. Effective rotation is positions % length. |
Usage example (from documentation)
BitVecRotateRight(&flags, 3);Success
Returns to the caller. Each previous bit at index i is now at index (i + length - positions) % length. No bits are lost; 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:1189:
}
void BitVecRotateRight(BitVec *bv, u64 positions) {
ValidateBitVec(bv);
if (positions == 0 || bv->length == 0) { DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecRotateRight\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
// Rotate right by 1 position
BitVecRotateRight(&bv, 1);
// Expected result: 1101 (1011 rotated right by 1)
// Test rotate by 0
BitVecPush(&bv, true);
BitVecRotateRight(&bv, 0);
result = result && (BitVecGet(&bv, 0) == true);
// Test large rotate amount
BitVecRotateRight(&bv, 1000);
result = result && (BitVecLen(&bv) == 2); // Rotate left by 3, then right by 3
BitVecRotateLeft(&bv, 3);
BitVecRotateRight(&bv, 3);
// Should restore original
BitVecPush(&bv, pat[i]);
}
BitVecRotateRight(&bv, 2);
bool result = true; bool test_rotate_right_frees_temp_clone(void);
bool test_rotate_right_frees_temp_clone(void) {
WriteFmt("Testing BitVecRotateRight frees its temp clone (1157:5)\n");
DebugAllocator dbg = DebugAllocatorInit(); BitVecPush(&bv, true);
BitVecRotateRight(&bv, 1);
// Correctness sanity: 1011 rotated right by 1 -> 1101.
// function-level ValidateBitVec; a NULL handle must abort.
static bool test_rotate_right_null_aborts(void) {
BitVecRotateRight((BitVec *)NULL, 1);
return false;
}
Last updated on