Rotating Bits Within a Fixed Width
A bit rotation moves bits around the ends of a field without discarding the bits carried past one side.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Rotate four positions
Rotating 1001 left once within four bits gives 0011. Shifting left and truncating instead gives 0010. Left shift moves the leading bit out, and rotation returns it to the low end.
Keep the width explicit
A rotation by four within this four-bit field returns the original. Rotating a byte by four swaps its two nibbles. Remainder arithmetic normalizes the distance against the width.[1]
Return a discarded bit at the other boundary
Rotate the eight-bit word 10010011 left by two. The leading bits 10 wrap to the low positions and the other six move left. The result is 01001110. A shift with truncation would leave the last two bits zero. See rotating a sequence.
Input: 10010011
Shifted: 01001100
Wrapped: 00000010
Combined: 01001110
Normalize rotation distance within the width
Rotating left eight reproduces the input and rotating left ten agrees with left two. For a fixed positive width, reduce the distance modulo that width. A zero reduced distance may need a special case in languages where a full-width shift is invalid.