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:1134:
}
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
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmtLn("Testing BitVecRotateRight");
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
Last updated on