bit_manipulation.rotate_bits

Author : Basuki Nath Date : 2025-10-04

Bit rotation helpers for 32-bit unsigned integers.

Functions

rotate_left32(→ int)

Rotate the lower 32 bits of x left by k and return result in 0..2**32-1.

rotate_right32(→ int)

Rotate the lower 32 bits of x right by k and return result in 0..2**32-1.

Module Contents

bit_manipulation.rotate_bits.rotate_left32(x: int, k: int) int

Rotate the lower 32 bits of x left by k and return result in 0..2**32-1.

>>> rotate_left32(1, 1)
2
>>> rotate_left32(1, 31)
2147483648
>>> rotate_left32(0x80000000, 1)
1
>>> rotate_left32(0x12345678, 4)
591751041
>>> rotate_left32(-1, 3)
Traceback (most recent call last):
    ...
ValueError: x must be a non-negative integer
>>> rotate_left32(1, -1)
Traceback (most recent call last):
    ...
ValueError: k must be non-negative
bit_manipulation.rotate_bits.rotate_right32(x: int, k: int) int

Rotate the lower 32 bits of x right by k and return result in 0..2**32-1.

>>> rotate_right32(2, 1)
1
>>> rotate_right32(1, 1)
2147483648
>>> rotate_right32(0x12345678, 4)
2166572391
>>> rotate_right32(-1, 1)
Traceback (most recent call last):
    ...
ValueError: x must be a non-negative integer
>>> rotate_right32(1, -3)
Traceback (most recent call last):
    ...
ValueError: k must be non-negative