Shift Register
A shift register moves its stored bits one position each clock edge, converting between serial and parallel data.
What it does
A shift register is a chain of flip-flops in which each stage's output feeds the next stage's input. On every clock edge the whole pattern moves one position. A new bit enters at one end and a bit falls off the other.
Serial and parallel
Shift registers convert between formats. A serial-in, parallel-out register accepts bits one at a time and, after n clocks, presents the full word on parallel outputs. A parallel-in, serial-out register does the reverse. This is how serial links transmit a word over a single wire.
Directions and types
A register can shift left, shift right, or, if bidirectional, either way under a control line. A universal shift register adds parallel load, giving four modes: hold, shift left, shift right, and load. Multiplexers on each stage select the mode.
Rotate and arithmetic shift
Feeding the output bit back to the input makes a rotate. In arithmetic right shift the sign bit is replicated to preserve a signed value, whereas logical shift brings in zeros.
Uses
Shift registers implement serial communication, delay lines, pseudo-random sequence generators, and the multiply and divide steps that shift operands one bit at a time.
In code
# shift right, bring in new_bit at the top
reg = (reg >> 1) | (new_bit << (width - 1))