What Is an LFSR?
Generating random numbers on a deterministic machine like a computer is complicated — this is where linear-feedback shift registers (LFSR) come in handy, and you can try them out with our LFSR calculator. With a little bit of math and some computer-science knowledge, you will learn everything you need about this special type of register.
First: what is a shift register? It is a type of electronic logic circuit that stores and transmits data by moving one bit in a particular direction of a register at every step — a basic type of computer memory. A shift register is a series of flip-flops connected in a sequential fashion, where the output of each flip-flop is the input of the next one. A flip-flop is a bistable circuit that stores exactly one bit, either 0 (low) or 1 (high).
If the input bit of the current state of the shift register is the result of some linear operation of the previous state, then we have a linear-feedback shift register. The linear function used for feedback is almost always the XOR (exclusive or) gate — or its negation, XNOR. XOR corresponds to bitwise addition modulo 2:
| A | B | A ⊕ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Fibonacci LFSR and Galois LFSR: A Compendium
The bit positions that influence the feedback are called the taps. The set of taps defines a feedback polynomial; for example, taps at positions 4 and 3 correspond to x⁴ + x³ + 1. There are two equivalent ways to wire the feedback:
- Fibonacci LFSR (external XOR) — the tapped bits are combined by a single chain of XOR gates outside the register. The result is fed back into the input bit while the register shifts, and the bit that falls off the far end is the output.
- Galois LFSR (internal XOR) — the output bit is shifted out and, whenever it is 1, it is XORed into the tapped bits inside the register. Galois LFSRs need no long XOR chain, so they are faster in hardware and software while producing the same sequence (in a different order).
Both forms share the same feedback polynomial and therefore the same period — the number of steps before the register returns to its starting state. An n-bit register has 2n possible states, but the all-zero state is a fixed point that must be avoided, so the longest achievable period is 2n − 1. When a register reaches this maximum, its taps form a primitive polynomial and the output is called a maximal-length sequence (m-sequence).
Examples of the Use of Linear-Feedback Shift Registers
- Pseudo-random number generation — fast, cheap PRNGs in hardware and embedded systems.
- Cryptography — the building blocks of many stream ciphers (though a bare LFSR alone is not secure).
- Spread-spectrum & CDMA — m-sequences and Gold codes used in GPS and mobile telephony.
- Error detection — cyclic redundancy checks (CRC) are computed with an LFSR.
- Digital counters & test patterns — LFSR counters and built-in self-test (BIST) circuits.
How to Use Our LFSR Calculator
- Choose the LFSR type — Fibonacci (external XOR) or Galois (internal XOR).
- Set the register size in bits (2 to 20). This determines the number of flip-flops.
- Enter the feedback taps as a comma-separated list of positions counted from the least-significant bit, e.g.
4,3. The calculator shows the matching feedback polynomial. - Enter the seed (initial state) as a binary string, e.g.
0001. It must be non-zero. - Choose how many clock steps to generate (up to 200).
The calculator returns the feedback polynomial, the full period, whether the sequence is maximal-length, the output bit stream, and a step-by-step table of every register state.
Example: a 4-bit Fibonacci LFSR with taps 4 and 3 (x⁴ + x³ + 1) starting from 0001 cycles through all 15 non-zero states before repeating — a maximal-length sequence of period 2⁴ − 1 = 15.
Conclusions
Linear-feedback shift registers turn a handful of flip-flops and XOR gates into a compact, deterministic generator of long, statistically random-looking bit streams. Picking the right taps — a primitive polynomial — is the key to getting the longest possible sequence out of a given register size.
Frequently Asked Questions (FAQ)
What is the difference between a Fibonacci and a Galois LFSR?
They use the same taps and produce the same maximal period, but the XOR gates are placed differently: outside the register (Fibonacci) or inside it (Galois). Galois is usually preferred in practice because it avoids a long chain of XOR gates.
Why can't the seed be all zeros?
The all-zero state is a fixed point: with only linear XOR feedback it maps to itself forever and never produces a sequence. That is why the maximum period is 2n − 1 rather than 2n.
What makes an LFSR "maximal-length"?
When the feedback taps correspond to a primitive polynomial over GF(2), the register visits every one of the 2n − 1 non-zero states before repeating. That longest possible output is called a maximal-length sequence, or m-sequence.
How do taps relate to the feedback polynomial?
Each tap position p contributes a term xp, and the constant term 1 is always present. Taps {4, 3} therefore give x⁴ + x³ + 1.
Are LFSRs cryptographically secure?
Not on their own — because they are linear, the taps and state can be recovered from a short run of output using the Berlekamp–Massey algorithm. Secure stream ciphers combine several LFSRs with non-linear functions.