BitVecShiftLeft
Description
Shift all bits in bitvector to the left by specified positions. New bits on the right (low end) are filled with zeros.
Parameters
| Name | Direction | Description |
|---|---|---|
bv |
in,out | Bitvector to shift in place. |
positions |
in | Number of positions to shift left. |
Usage example (from documentation)
BitVecShiftLeft(&flags, 3);Success
Returns to the caller. Each previous bit at index i is now at index i + positions (when in range); bits at indices < positions are now false; bits that shifted past length - 1 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:1064:
}
void BitVecShiftLeft(BitVec *bv, u64 positions) {
ValidateBitVec(bv);
if (positions == 0 || bv->length == 0) { DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecShiftLeft\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc)); // After shift left by 2: bits move to higher indices, lower indices filled with 0
// New: 0011 (bit 0=0, bit 1=0, bit 2=1, bit 3=0) - but we shift out the high bits
BitVecShiftLeft(&bv, 2);
// After shift left by 2, implementation should clear bits that shift out
// Test shift empty bitvec
BitVecShiftLeft(&bv, 5);
result = result && (BitVecLen(&bv) == 0); BitVecPush(&bv, true);
BitVecPush(&bv, false);
BitVecShiftLeft(&bv, 0);
result = result && (BitVecLen(&bv) == 2);
result = result && (BitVecGet(&bv, 0) == true);
// Test shift larger than length (should clear all bits)
BitVecShiftLeft(&bv, 10);
result = result && (BitVecLen(&bv) == 0); // Should clear when shifting everything out
BitVecPush(&bv, i % 2 == 0);
}
BitVecShiftLeft(&bv, 1);
result = result && (BitVecLen(&bv) == 1000);
// Shift left by 1, then right by 1 - should NOT restore original (data loss)
BitVecShiftLeft(&bv, 1);
BitVecShiftRight(&bv, 1); }
BitVecShiftLeft(&bv, 8);
result = result && (BitVecLen(&bv) == 0); BitVecPush(&bv, false);
BitVecShiftLeft(&bv, 2);
result = result && (BitVecLen(&bv) == 3);
result = result && (BitVecGet(&bv, 0) == false); // filled with 0
}
BitVecShiftLeft(&result, 100);
test_result = test_result && (BitVecLen(&result) == 1000);
// Test NULL bitvec pointer - should abort
BitVecShiftLeft(NULL, 1);
return false; DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmtLn("Testing BitVecShiftLeft");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc)); // After shift left by 2: bits move to higher indices, lower indices filled with 0
// New: 0011 (bit 0=0, bit 1=0, bit 2=1, bit 3=0) - but we shift out the high bits
BitVecShiftLeft(&bv, 2);
// After shift left by 2, implementation should clear bits that shift out
// Test shift empty bitvec
BitVecShiftLeft(&bv, 5);
result = result && (BitVecLen(&bv) == 0); BitVecPush(&bv, true);
BitVecPush(&bv, false);
BitVecShiftLeft(&bv, 0);
result = result && (BitVecLen(&bv) == 2);
result = result && (BitVecGet(&bv, 0) == true);
// Test shift larger than length (should clear all bits)
BitVecShiftLeft(&bv, 10);
result = result && (BitVecLen(&bv) == 0); // Should clear when shifting everything out
BitVecPush(&bv, i % 2 == 0);
}
BitVecShiftLeft(&bv, 1);
result = result && (BitVecLen(&bv) == 1000);
// Shift left by 1, then right by 1 - should NOT restore original (data loss)
BitVecShiftLeft(&bv, 1);
BitVecShiftRight(&bv, 1); }
BitVecShiftLeft(&bv, 8);
result = result && (BitVecLen(&bv) == 0); BitVecPush(&bv, false);
BitVecShiftLeft(&bv, 2);
result = result && (BitVecLen(&bv) == 3);
result = result && (BitVecGet(&bv, 0) == false); // filled with 0
}
BitVecShiftLeft(&result, 100);
test_result = test_result && (BitVecLen(&result) == 1000);
// Test NULL bitvec pointer - should abort
BitVecShiftLeft(NULL, 1);
return false;
Last updated on