Skip to content
BetterDL

Rotate

Also called: rotation, rotate right, rotate left, ROR, ROL, circular shift, cyclic shift, bit rotation

A shift in which the bit leaving one end re-enters at the other end, so no bit is lost. After n rotations an n-bit value returns to its start.

A rotate (or circular shift) is a shift with the ends joined. The bit that falls off one end comes back in at the other.

  • Rotate right: every bit moves one place toward Q0, and the old Q0 goes into Q3. 1101 → 1110.
  • Rotate left: every bit moves one place toward Q3, and the old Q3 goes into Q0. 1101 → 1011.

No information is lost, so rotations are reversible: rotating right by k undoes rotating left by k. After n single-place rotations, an n-bit register is back where it started. Rotating by k places is the same as rotating by k mod n.

In hardware: a shift register with its serial input wired to its own serial output. A shift-right register with SI = Q0 rotates right. A universal shift register does the same with SIR wired to Q0.

Where it is used:

  • A register that rotates forever, started with a single 1, is a ring counter.
  • Cryptography and hashing algorithms mix bits with rotations.
  • Most processors have rotate instructions (ROL and ROR), sometimes with the carry flag included in the loop.

Compare a plain shift, where the bit that leaves is lost and a fixed value (0, or the sign bit) enters.

Worked examples

Example

Rotate left by two

Rotate the 8-bit value 01101001 left by two places.

  1. 1.

    The top two bits, 01, will wrap round to the right-hand end.

  2. 2.

    The remaining bits 101001 move up two places.

  3. 3.

    Result: 101001 followed by 01 = 10100101.

  4. 4.

    Check: rotating right by two brings back 01101001.

Example

When does the pattern repeat?

A 6-bit register holds 100100 and rotates right at every edge. After how many edges does it first return to 100100?

  1. 1.

    Edge 1: 010010. Edge 2: 001001. Edge 3: 100100.

  2. 2.

    It returns after 3 edges, not 6, because the pattern itself repeats every 3 bits.

  3. 3.

    In general the answer divides n; it is n only when the pattern has no shorter repeat.

Common mistakes

  • Shifting in a 0 instead of the bit that left. That is a shift, not a rotate.

  • Rotating the wrong way. Right moves bits toward Q0; left moves them toward Q3.

  • Assuming every pattern needs exactly n rotations to come back. Patterns with internal repeats come back sooner.

Practice Rotate

Interactive questions with instant feedback and a worked solution for every wrong answer.

Learn it step by step

Rotate is taught in Registers and Adders and ALUs.