The Fibonacci Linear Feedback Shift Register (LFSR) is a digital circuit which generates a stream of random-looking bits. It consists of several 1-bit flip-flops in a row, with certain positions designated as 'tapped'. In each iteration, the values in the tapped positions are XORed together (i.e., summed modulo 2) and the result is held in reserve. Then the bits are shifted to the right. The rightmost bit falls off the register and becomes the output, while the reserve bit is input on the left.

Maximal LFSR

The output stream of a LFSR is periodic; it repeats after some number of iterations. A maximal LFSR has a period of 2^n - 1, where n is its number of bits. This only occurs if the tapped positions are correctly chosen; otherwise the period will be less. If the LFSR is maximal, it will cycle through all possible internal states (except all zeroes) exactly once before repeating.

For more information about maximal Fibonacci LFSRs (primitive polynomials), see this article: https://en.wikipedia.org/wiki/Linear_feedback_shift_register#Fibonacci_LFSRs


You must write a function that takes as its arguments a list representing the initial internal state of an n-bit LFSR (a list of length n made of 0 and at least one 1), and a list of tapped positions. The tapped positions are input as a list with either of these general formats (brackets, commas, and spaces are shown for convenience and are entirely optional):

Format A: [0, 0, 1, 1]

Format B: [3, 4]

Both formats declare that the 3rd and 4th positions are tapped.

The function must produce exactly one complete cycle of output of the LFSR (either as function output, or printed to the screen), as well as a Boolean evaluation of whether or not the LFSR period is maximal.

If you prefer, you may use two functions to produce the LFSR output and maximality evaluation.


Per usual code golf rules, your score is the number of characters in your code. Make sure your code evaluates these examples correctly:

Example 1

State: [1, 0, 1, 0]

Taps: [3,4]

Output: [0,1,0,1,1,1,1,0,0,0,1,0,0,1,1] and True (since the period is 2^n - 1 = 15)

Example 2

State: [1, 0, 1, 0, 1]

Taps: [3, 5]

Output: [1,0,1,0,1,0,0,0,0,1,0,0,1,0,1,1,0,0,1,1,1,1,1,0,0,0,1,1,0,1,1] and True (since the period is 2^n - 1 = 31)

Example 3

State: [1, 0, 1, 0, 1]

Taps: [4, 5]

Output: [1,0,1,0,1,1,1,1,1,0,0,0,0,1,0,0,0,1,1,0,0] and False (since the output has period 21, which is not equal to 2^n - 1)

  • \$\begingroup\$ Is there any leniency regarding the input format? For example, would requiring state input be in the form 10101 rather than [1, 0, 1, 0, 1] be acceptable? \$\endgroup\$
    – primo
    Jun 6, 2014 at 5:21
  • \$\begingroup\$ Certainly, the list with square brackets was only a suggested input format. Spaces and commas should only be used if they are convenient in your language. \$\endgroup\$
    – phosgene
    Jun 6, 2014 at 5:22
  • \$\begingroup\$ May we use 0-indexed taps rather than 1-indexed? \$\endgroup\$ Jun 6, 2014 at 7:23
  • \$\begingroup\$ Sure, any unambiguous input format is fine. \$\endgroup\$
    – phosgene
    Jun 6, 2014 at 7:48

4 Answers 4


GolfScript, 38 characters


You may declare it as a named function if you enclose the code in {<code>}:F. It takes the top two arguments from the stack and pushes the results back to the stack, e.g. (test it online):

[1 0 1 0 1] [3 5]


# => yields [1 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 0 0 1 1 1 1 1 0 0 0 1 1 0 1 1] 1

Annotated code:

:f;          # save tap positions (1 indexed) to variable f
:i           # and the initial state to variable i
[{           # the [{...}do;] loop generates an array using the loop
             #   top of stack is the current state
  0          #   push zero (accumulator)
  {          #   {...}f/ loops over all tap positions
    (        #     make the current position zero-indexed
    2$=      #     take the corresponding bit from the current state (2$)
    ^        #     xor with the accumulator
  }f/        #   end of loop
  \+         #   prepend the new bit to the current state
  )\         #   and shift out the last bit (output)
  .i=        #   compare the current state to input
  !          #   if not equal then loop
}do;]        # end of loop - discard the state
.,)          # length of the output + 1
2i,?         # 2^length of initial state
=            # are those two numbers equal?
  • 1
    \$\begingroup\$ People have probably used LFSRs longer than that code... \$\endgroup\$
    – phosgene
    Jun 6, 2014 at 6:44

Perl - 78 bytes

sub f{($_,$t)=@_;s//($_&$t)%9&1/e,@o=(@o,chop)until$o{$_}++;@o,$/,@o+1>>y///c}

Input is expected as two binary strings, one representing the current state, and the other the taps. For example, the input ('10101', '00011') corresponds to an initial state with the 1st, 3rd, and 5th bits set (one indexed), with taps on the 4th and 5th bit.

For the truth condition, a 0 or 1 is returned, separated by a newline.

Note: The bit counting logic above, ($_&$t)%9&1, will fail if the total number of bits is more than 16, and/or the number of taps is greater than 8. This can be replaced by ($_&$t)=~y|0||c&1 at the cost of 6 bytes.

Test Case 1

print f('1010', '0011');



Test Case 2

print f('10101', '00101');



Test Case 3 Input:

print f('10101', '00011');


  • \$\begingroup\$ ($_,$t) looks like an emoticon. Perhaps a mixture of greed and regret. That gives me an idea... \$\endgroup\$
    – phosgene
    Jun 6, 2014 at 6:36

Javascript (E6) 144 157 169

Edit Output as string instead of array



( P => { // Inner block, spare a final 'return'
  b = s.length,
  i = s = P(s,2), t=P(t,2);
  for (o='';!o||i-s;) {
    for (p=q=s&t;q>>=1;p^=q);
    o += s & 1,
    s = s >> 1 | (p&1) << b-1
} ) (parseInt)
|| [!!o[(1<<b)-2], o]  


State and Tapped position input as strings of bits (format A) Return an array with true/false and a string of output bits


Javascript console in firefox,

F('1010', '0011') [true, "010111100010011"]

F('10101', '00101') [true, "1010100001001011001111100011011"]

F('10101', '00011') [false, "101011111000010001100"]


Go, 208 bytes

func f(S,T[]int)(o[]int,m bool){s:=S
for len(o)<1||!Equal(s,S){l,L:=S[T[0]],len(S)-1
for _,i:=range T[1:]{l^=S[i]}
return o,O&(O-1)<1}

Attempt This Online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.