Skip to content
BitVecRotateLeft

BitVecRotateLeft

Description

Rotate all bits in bitvector to the left by specified positions. Bits that fall off the high end wrap around to the low end.

Parameters

Name Direction Description
bv in,out Bitvector to rotate in place.
positions in Number of positions to rotate left. Effective rotation is positions % length.

Usage example (from documentation)

  BitVecRotateLeft(&flags, 5);

Success

Returns to the caller. Each previous bit at index i is now at index (i + 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)
    }
    
    void BitVecRotateLeft(BitVec *bv, u64 positions) {
        ValidateBitVec(bv);
        if (positions == 0 || bv->length == 0) {
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecRotateLeft\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Rotate left by 2 positions
        BitVecRotateLeft(&bv, 2);
    
        // Expected result: 1110 (1011 rotated left by 2)
    
        // Test rotate empty bitvec
        BitVecRotateLeft(&bv, 5);
        result = result && (BitVecLen(&bv) == 0);
        // Test rotate by length (should be no-op)
        BitVecPush(&bv, false);
        BitVecRotateLeft(&bv, 2);
        result = result && (BitVecLen(&bv) == 2);
    
        // Rotate left by 3, then right by 3
        BitVecRotateLeft(&bv, 3);
        BitVecRotateRight(&bv, 3);
    
        // Test rotate by multiple of length (should be no-op)
        BitVecRotateLeft(&bv, 16); // 8 * 2
    
        bool unchanged = true;
        BitVecPush(&bv, true); // 5 bits: 10101
    
        BitVecRotateLeft(&bv, 2);
        // 10101 -> 10110 (rotated left by 2)
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Test NULL pointer for rotate - should abort
        BitVecRotateLeft(NULL, 3);
    
        return false;
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmtLn("Testing BitVecRotateLeft");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Rotate left by 2 positions
        BitVecRotateLeft(&bv, 2);
    
        // Expected result: 1110 (1011 rotated left by 2)
    
        // Test rotate empty bitvec
        BitVecRotateLeft(&bv, 5);
        result = result && (BitVecLen(&bv) == 0);
        // Test rotate by length (should be no-op)
        BitVecPush(&bv, false);
        BitVecRotateLeft(&bv, 2);
        result = result && (BitVecLen(&bv) == 2);
    
        // Rotate left by 3, then right by 3
        BitVecRotateLeft(&bv, 3);
        BitVecRotateRight(&bv, 3);
    
        // Test rotate by multiple of length (should be no-op)
        BitVecRotateLeft(&bv, 16); // 8 * 2
    
        bool unchanged = true;
        BitVecPush(&bv, true); // 5 bits: 10101
    
        BitVecRotateLeft(&bv, 2);
        // 10101 -> 10110 (rotated left by 2)
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Test NULL pointer for rotate - should abort
        BitVecRotateLeft(NULL, 3);
    
        return false;
Last updated on