Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??

Here is a bit more on what an LFSR is.

A LFSR is a register of N bits. Two or more positions are designated as "taps" and the bit values from those positions are XOR'd together to produce a new bit. This bit is placed to the left of the register and all bits are then shifted to the right with the previous right-most bit being returned as the output.

       4   3   2   1   0   <- tap location numbers
  ,->| 0 | 1 | 0 | 1 | 0 | -> Output
  |  +---+---+---+---+---+
  |        |           |
  |        |           |
  |        `->--XOR--<-'
  |              |

You repeat this process forever, producing a somewhat random stream of bits as a result.

A good LFSR will repeat the pattern of bits produced only after (2**N)-1 interations. For each size of LFSR, there are positions for the taps that result in such a "good" LFSR. Unless, of course, you seed it with a value of 0, which will always stay 0 since the XOR of any number of 0 bits is another 0 bit. Note, that is why the period can't be larger than (2**N)-1 since there are 2**N possible states and one of them (the value 0) can't be part of the loop.

You can go to and search for LFSR to get more information on these. The person who pointed these out to me also pointed me to the book Applied Cryptography.

From my quick introduction to these, I'm starting to think that most of the material on them is a bit sloppy. (:

It seems to me that if you have an odd number of taps, then either a seed of 0 or of all 1 bits would never change (the XOR of an odd number of 1 bits is another 1 bit). But most of the "good" arrangements of taps have an odd number of taps [ which means that the maximum period for an odd number of taps would be (2**N)-2 ].

So I think we should really be using XNOR instead of XOR. Though I'm reluctant to jump to many conclusions since I've just barely learned of them. :)

        - tye (but my friends call me "Tye")

In reply to (tye)Re: LFSR golf by tye
in thread LFSR golf by tye

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.