Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksToolsOpen the lab →

How to build an LFSR (linear feedback shift register)

A linear feedback shift register (LFSR) is a shift register whose new input bit is the XOR or XNOR of some of its own bits, so it steps through a long, fixed sequence of states that looks random. This live 4-bit one feeds Q3 XNOR Q2 back into Q0 and visits 15 different states before it repeats.

Every clock the bits move one place along, Q0 to Q1 to Q2 to Q3, and the feedback gate computes the bit that enters at Q0. The flip-flops it reads are the taps, and the choice of taps decides how long the sequence is. Taps 4 and 3 (the polynomial x4 + x3 + 1) give the longest run possible, 24 - 1 = 15 states. One state is always left out: with XOR feedback 0000 would shift in 0 forever, and with XNOR, as here, 1111 would shift in 1 forever. XNOR is handy because the clear input then gives a valid starting state, 0000. The register has only four flip-flops and one gate, whatever the sequence, which is why LFSRs generate test patterns for chips, scramble data on serial links, compute CRCs (the same shift and XOR, with the message fed in) and serve as very cheap counters. Two laps of the 15 states are checked, all four bits after every clock, before the page is built.

Live circuit: click the inputs to toggle them and watch the outputs update. It runs the real simulator, right here on the page.

Open this circuit in the lab →

What you need

Step by step

  1. Place a clock CLK, an input CLR, four D flip-flops with clear, and probes Q0 to Q3 on their outputs.
  2. Chain them into a shift register: Q0 drives D of the second flip-flop, Q1 the third and Q2 the fourth. All four share CLK and CLR.
  3. Add the feedback: an XNOR of Q3 and Q2 drives D of the first flip-flop.
  4. Pulse CLR to start from 0000, then run the clock. The state walks through 15 different values and comes back to 0000; Q3 is the pseudo-random output bit stream.

The 15 states, clock by clock

Clocks since CLRQ0 Q1 Q2 Q3Next Q0 = Q3 XNOR Q2
00 0 0 01
11 0 0 01
21 1 0 01
31 1 1 00
40 1 1 11
51 0 1 11
61 1 0 10
70 1 1 00
80 0 1 11
91 0 0 10
100 1 0 01
111 0 1 00
120 1 0 10
130 0 1 00
140 0 0 10
150 0 0 01

After 15 clocks the register is back at 0000, so the period is 15 = 24 - 1, and 1111 never appears. Reading Q3 down the table gives the output stream 000011101100101, which then repeats. Longer registers with the right taps run much longer: 216 - 1 = 65,535 states from 16 flip-flops and a few XNOR gates.

You can also read the full theory in the lesson on shift registers. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.

Build more parts from gates

half adderfull adderhalf subtractorfull subtractor2:1 multiplexer4:1 multiplexerXOR gate from NAND gates2-to-4 decoderSR latchD flip-flopJK flip-flopT flip-flop4-bit shift register4-bit ripple carry adderD latch1-to-4 demultiplexer4-bit magnitude comparator4-bit ring counter4-bit Johnson counter4-to-2 priority encoder4-bit synchronous counter3-to-8 decoder8-to-1 multiplexer4-bit carry lookahead adderhalf adder from NAND gates2-bit comparatorBCD to 7-segment decoder4-bit up/down counterdecade counter2-bit multiplier4-bit ALU4-bit array multiplierNOT, AND, OR and NOR from NANDNOT, OR, AND and XOR from NORfull adder from NAND gatesSR latch from NAND gatesparity generator and checker4-bit adder/subtractorBCD adder4-bit universal shift registermod-6 counter4-bit ripple counterbinary to Gray code converterhalf adder from NOR gatesfull adder from NOR gates4-bit register with parallel load1011 sequence detectorbinary to BCD converterD flip-flop from NAND gates1-bit ALUCMOS inverterCMOS NAND gateCMOS NOR gatePISO shift registerSR flip-flop from NAND gatesBCD to excess-3 code converterHamming (7,4) code encoder and correctortraffic light controller4-bit barrel shifterCMOS AND gateCMOS OR gateCMOS XOR gate1-bit magnitude comparatorhalf subtractor from NAND gatesfull subtractor from NAND gates8-to-3 encoderGray to binary converter4-to-16 decoderfull adder using 4:1 multiplexersedge detectorserial adder4-bit carry-select adder4-bit Kogge-Stone adder4-bit carry-skip adder4x4 Wallace tree multiplier8-bit adderfrequency divider (by 2, 4 and 3)switch debouncer (SR latch)3-bit Gray code counterMealy machine (1011 detector)master-slave JK flip-flop (NAND)half subtractor from NOR gates1-to-8 demultiplexerexcess-3 to BCD code convertervending machine FSM4-bit carry-save adder3-bit synchronous counter (JK)CMOS transmission gatetri-state buffer (CMOS)2-to-1 mux from transmission gatesmod-5 counterflip-flop conversion (JK to D)8-bit ALUdecimal to BCD encoderBCD to decimal decoder4-to-1 mux from 2-to-1 muxesXOR gate from NOR gates74LS138 decoderBCD subtractor

Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.

Open the 4-bit LFSR in the lab →

Frequently asked

What is an LFSR?

A linear feedback shift register: a shift register whose input bit is the XOR (or XNOR) of some of its own outputs, called taps. Each clock shifts the register and feeds that bit back in, so it steps through a fixed sequence of states that looks random.

Why does an n-bit LFSR have 2^n - 1 states and not 2^n?

Because one state maps to itself. With XOR feedback, all zeros shifts in another 0 forever; with XNOR, all ones shifts in another 1. A maximal-length LFSR visits every other state, 2^n - 1 of them, before it repeats.

Which taps give a maximal-length LFSR?

For feedback into the first stage of an n-bit register, these tap sets give all 2^n - 1 states, with XOR or XNOR alike: 3 bits (3, 2), 4 bits (4, 3), 5 bits (5, 3), 6 bits (6, 5), 7 bits (7, 6), 8 bits (8, 6, 5, 4) and 16 bits (16, 15, 13, 4). Each was checked here by running the register through its whole period.

What is the difference between an XOR and an XNOR LFSR?

Only the stuck state and the sequence. An XOR LFSR locks up at all zeros, so it must be seeded with something else; an XNOR LFSR locks up at all ones, so a plain reset to zeros is a valid start. With the same taps both have the same period.

What are LFSRs used for?

Pseudo-random test patterns for chips, scrambling and whitening data on serial links, CRC error detection, spreading codes such as the GPS C/A code (built from two 10-bit LFSRs), and counters that need very little logic.

Is an LFSR a secure random number generator?

No. Its output is completely predictable: the Berlekamp-Massey algorithm recovers the shortest LFSR that produces a sequence from just 2n of its bits. LFSRs are fine for testing and noise-like signals, not for cryptography.