---
title: Shift registers, LFSRs and CRCs
heading: Moving bits in a line, and the sequences a little feedback makes
eyebrow: C7 · Lesson 31 · Shift registers, LFSRs and CRCs
xp: 40
---

::: hook
Chain flip-flops so each one's input is the previous one's output, and on every clock edge the bits march one place along: a shift register. That's how bits cross a serial link one at a time and turn back into bytes at the other end. Feed a couple of the bits back through an XOR gate and the register starts producing a long sequence that looks random but repeats exactly: a **linear-feedback shift register**. LFSRs generate test patterns and scramble data on every high-speed link, and the same circuit computes the CRC that checks every Ethernet frame and file you download.
:::

::: play
- A 4-bit LFSR: on each clock the bits shift from $Q_0$ toward $Q_3$, and the new $Q_0$ is the XNOR of $Q_3$ and $Q_2$.
- Read the four bits as a number on each clock: 0000, 0001, 0011, 0111, 1110, ... It visits 15 different states before repeating, every state except 1111.
- Four flip-flops and one gate produce a 15-step pseudo-random sequence; 32 flip-flops would give over four billion steps.
:::
:::

```sim logic
clock CLK 8
output Q3, Q2, Q1, Q0
always @(posedge CLK) begin
  Q0 <= ~(Q3 ^ Q2);
  Q1 <= Q0;
  Q2 <= Q1;
  Q3 <= Q2;
end
wave: CLK, Q3, Q2, Q1, Q0
time: 136
```

::: predict
## A 16-bit LFSR with the right feedback taps cycles through how many states before repeating?

- [ ] 16
- [ ] 256
- [x] 65,535
- [ ] 65,536

::: reveal
65,535: $2^{16} - 1$. A **maximal-length** LFSR visits every state but one, the one it would get stuck in (all zeros for XOR feedback, all ones for XNOR). Not every choice of taps is maximal; the right ones come from primitive polynomials.
:::
:::

::: nameit
## Shift registers

- **Serial in, parallel out (SIPO):** bits arrive one per clock and, after n clocks, the n-bit word is available at once: a serial receiver.
- **Parallel in, serial out (PISO):** load a word, then shift it out one bit per clock: a transmitter.
- At a clock of f, n bits take n/f to cross.

## Counters from shift registers

- **Ring counter:** the last output fed back to the first, with a single 1 circulating: n states from n flip-flops, but each state decodes with no logic.
- **Johnson counter:** the last output *inverted* and fed back: 2n states from n flip-flops (0000, 1000, 1100, 1110, 1111, 0111, ...).

## Linear-feedback shift registers

The feedback is the XOR of selected bits, the **taps**. With taps matching a **primitive polynomial** (for 4 bits, $x^4 + x^3 + 1$: taps 4 and 3), the sequence has the maximal length $2^n - 1$. Each output bit looks random (as many 1s as 0s, give or take one), but the sequence is completely predictable from the state. Uses: pseudo-random test patterns (built-in self-test), scramblers that keep serial links from sending long runs of 0s, and spread-spectrum codes.

## CRCs

A **cyclic redundancy check** treats a message as a polynomial over bits and divides it by a fixed **generator polynomial**, using XOR in place of subtraction; the remainder, n bits for a degree-n generator, is appended. The receiver divides again and finds a remainder of zero unless the message was corrupted. In hardware the division is an LFSR whose taps are the generator's terms: one bit of message per clock. A CRC with an n-bit remainder catches every burst error up to n bits long.
:::

::: example
## Compute the 3-bit CRC of the message 1101 with generator 1011 ($x^3 + x + 1$).

::: solution
1. **Append three zeros.** The message becomes 1101000.
2. **Divide by 1011 with XOR.** The first step is
   $$1101 \oplus 1011 = 0110$$
   then bring down bits and repeat until the message is used up.
3. **Read the remainder.** The remainder is 001, so 1101001 is sent.
4. **Check.** Dividing 1101001 by 1011 leaves 000.

**Answer:** the CRC is 001, and 1101001 is sent.
:::
:::

::: example
## A serial link runs at 115,200 bits per second with a start and a stop bit around each 8-bit byte. How many bytes per second does it carry?

::: solution
1. **Bit times per byte.** Each byte takes 10 bit times: a start bit, 8 data bits and a stop bit.
2. **Divide.**
   $$\frac{115\,200}{10} = 11\,520$$

**Answer:** 11,520 bytes per second.
:::
:::

::: example
## A 4-bit LFSR shifts left and feeds back the XOR of its top two bits, which implements $x^4 + x^3 + 1$. Starting at 0001, how many clocks until it returns?

::: solution
1. **Run the sequence.** 0001, 0010, 0100, 1001, 0011, 0110, 1101, 1010, 0101, 1011, 0111, 1111, 1110, 1100, 1000, then back to 0001.
2. **Count the states.** That's 15 states, every nonzero pattern once.
3. **Why 15.** A primitive polynomial of degree n gives the maximum period $2^n - 1$; only the all-zero state is missing, and it must be avoided because it feeds back zeros forever.
4. **Uses.** Such sequences make cheap pseudorandom test patterns and scramblers.

**Answer:** 15 clocks.
:::
:::

::: check
given: n = 4 | 8 | 16 | 24
answer: 2^n - 1
tolerance: 0

## How many states does a maximal-length {n}-bit LFSR cycle through?

::: right
Right. $2^n - 1$ = {answer}.
:::

::: misses
- [2^n] One state is excluded: the one the register would stay stuck in.
- [any] Not yet. Every state but one.
:::
:::

::: check
given: n = 3 | 4 | 8
answer: 2*n
tolerance: 0.1%

## How many states does a Johnson counter built from {n} flip-flops have?

::: right
Right. 2n = {answer}.
:::

::: misses
- [n] That's a ring counter; inverting the feedback doubles the cycle.
- [2^n] A binary counter reaches $2^n$; a Johnson counter only 2n.
- [any] Not yet. Fill with 1s, then empty them.
:::
:::

::: check
given: n = 8 | 32 | 1000; f = 1 | 50
answer: n/(f*1e6)*1e6
unit: µs
tolerance: 0.5%

## A shift register clocked at {f} MHz receives {n} bits serially. How long does it take?

::: right
Right. n/f = {answer}.
:::

::: misses
- [any] Not yet. One bit per clock.
:::
:::

::: check
given: s = 4 | 6 | 9 | 11; let b3 = floor(s/8) - 2*floor(s/16); let b2 = floor(s/4) - 2*floor(s/8)
answer: 2*s - 16*floor(2*s/16) + abs(b3 - b2)
tolerance: 0.1%

## A 4-bit LFSR shifts left, dropping bit 3, and puts $Q_3 \oplus Q_2$ into bit 0. Its state is {s} (in decimal). What is the next state?

::: right
Right. Shift left within four bits and bring in $Q_3 \oplus Q_2$: {answer}.
:::

::: misses
- [2*s - 16*floor(2*s/16)] Don't forget the feedback bit coming into bit 0.
- [any] Not yet. Write {s} in binary, shift, and compute the new bit.
:::
:::

::: check
given: rate = 9600 | 115200 | 1000000; bits = 10 | 11
answer: rate/bits
tolerance: 0.5%

## A serial link sends {bits} bit times per byte (start, data, parity and stop bits) at {rate} bits per second. How many bytes per second?

::: right
Right. {answer}.
:::

::: misses
- [rate/8] Count the framing bits, not just the 8 data bits.
- [any] Not yet. Divide by the bit times per byte.
:::
:::

::: check
answer: 1
base: 2
width: 3

## What is the 3-bit CRC of the message 1101 with generator 1011?

::: right
Right. 001: the message is sent as 1101001.
:::

::: misses
- [3] Append three zeros before dividing.
- [any] Not yet. Append 000, then divide by 1011 using XOR.
:::
:::

::: check
kind: parts
given: n = 4 | 7 | 16; r = 9600 | 115200

## A maximal-length LFSR has n = {n} bits; a serial link runs at {r} bits per second with start and stop bits.

::: part
answer: 2^n - 1
tolerance: 0

### What's the LFSR's period?

::: misses
- [2^n] The all-zero state is excluded.
- [any] $2^n - 1$.
:::
:::

::: part
answer: r/10
unit: bytes/s
tolerance: 0.5%

### How many bytes per second does the link carry?

::: misses
- [r/8] Each byte takes 10 bit times with its start and stop bits.
- [any] $\frac{r}{10}$.
:::
:::
:::

::: check
kind: derivation
vars: n = 2..64, f = 1000..1000000000

## A maximal-length n-bit LFSR runs from a clock at f.

::: step
answer: 2^n - 1

### What's its period, in clocks?
:::

::: step
answer: (2^n - 1)/f

### How long before it repeats?
:::

::: right
Right. A 64-bit LFSR at 1 GHz doesn't repeat for centuries.
:::
:::

::: explain
## Why does a maximal-length LFSR visit every state except one?

::: model
An LFSR's next state depends only on its present state, so once it returns to any state it repeats the same cycle forever. With XOR feedback, the all-zeros state maps to itself: every bit is 0, so the feedback is 0 and nothing changes. That state forms a cycle of length one by itself, so the longest cycle the other states can form contains all $2^n - 1$ of them. Choosing the taps from a primitive polynomial guarantees the register achieves that longest cycle. (With XNOR feedback the stuck state is all ones instead.)
:::
:::

::: practice

::: check
given: n = 3 | 5 | 10
answer: n
tolerance: 0.1%

## How many states does a ring counter of {n} flip-flops have?

::: misses
- [any] One per flip-flop.
:::
:::

::: check
given: n = 8 | 16 | 32
answer: n
tolerance: 0.1%

## How many clocks does a PISO register need to send an {n}-bit word?

::: misses
- [any] One bit per clock.
:::
:::

::: check
given: d = 8 | 16 | 32
answer: d
tolerance: 0.1%

## How many bits long is the CRC from a generator polynomial of degree {d}?

::: misses
- [any] The remainder has as many bits as the degree.
:::
:::
:::
