Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I ran into the concept of a Linear Feedback Shift Register so I wrote one:

#!/usr/bin/perl -w use strict; # Generate a closure that implements a Feedback Shift Register: # Usage: # my $fsr= genFSR( $bits, $seed, \&feedBack, [@taps] ); # my $nextBit= $fsr->(); sub genFSR { my( $bits, $seed, $code, $avTaps )= @_; return sub { my $ret= $seed & 1; my $b= $code->( map $seed & 1<<$_ ? 1 : 0, @$avTaps ); $seed >>= 1; $seed |= (1&$b)<<$bits; return $ret; }; } # XOR all arguments: sub xorList { my $b= 0; $b ^= pop while @_; return $b; } # Generate a Linear Feedback Shift Register closure: # Usage: # my $lfsr= genFSR( $bits, $seed, [@taps] ); # $nextBit= $lfsr->(); sub genLFSR { my( $bits, $seed, $avTaps )= @_; return genFSR( $bits, $seed, \&xorList, $avTaps ); } # Test the above using command-line arguments: # Usage: lfsr nBits Seed tap tap [tap [...]] # perl lfsr.pl 5 10 3 5 # perl lfsr.pl 5 0b1010 3 5 # Same if using perl 5.6 or later # Both of the above use: # genLFSR( 5, 10, [3,5] ); my $bits= shift; my $seed= shift; $seed= oct($seed) if $seed =~ /^0/; my $lfsr= genLFSR( $bits, $seed, [@ARGV] ); while( 1 ) { print join( " ", map $lfsr->(), 1..$bits ), $/; }

To make playing golf on this easier, you only have to implement a Linear FSR, and you don't have to accept the list of taps from an anonymous array.

So write a function that generates a closure that implements an N-bit LFSR with 2 or more taps:

my $lfsr= golfGenLFSR( $nBits, $iSeed, $tap0, $tap1, $tap2 ); my $nextBit= $lfsr->();
You may choose to use packed bit strings instead of integers (see vec).

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

In reply to LFSR golf by tye

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (5)
    As of 2014-07-29 03:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (211 votes), past polls