r/dailyprogrammer • u/jnazario 2 0 • Jan 17 '18
[2018-01-17] Challenge #347 [Intermediate] Linear Feedback Shift Register
Description
In computing, a linear-feedback shift register (LFSR) is a shift register whose input bit is a linear function of its previous state. The most commonly used linear function of single bits is exclusive-or (XOR). Thus, an LFSR is most often a shift register whose input bit is driven by the XOR of some bits of the overall shift register value.
The initial value of the LFSR is called the seed, and because the operation of the register is deterministic, the stream of values produced by the register is completely determined by its current (or previous) state. Likewise, because the register has a finite number of possible states, it must eventually enter a repeating cycle.
Your challenge today is to implement an LFSR in software.
Example Input
You'll be given a LFSR input on one line specifying the tap positions (0-indexed), the feedback function (XOR or XNOR), the initial value with leading 0s as needed to show you the bit width, and the number of clock steps to output. Example:
[0,2] XOR 001 7Example Output
Your program should emit the clock step and the registers (with leading 0s) for the input LFSR. From our above example:
0 001
1 100
2 110
3 111
4 011
5 101
6 010
7 001Challenge Input
[1,2] XOR 001 7
[0,2] XNOR 001 7
[1,2,3,7] XOR 00000001 16
[1,5,6,31] XOR 00000000000000000000000000000001 16Challenge Outut
(Only showing the first two for brevity's sake.)
0 001
1 100
2 010
3 101
4 110
5 111
6 011
7 0010 001
1 000
2 100
3 010
4 101
5 110
6 011
7 001 Further Reading
- Tutorial: Linear Feedback Shift Registers (LFSRs) - Part 1 from the IEEE
- Linear Feedback Shift Registers Theory and Applications - some lecture notes from Kwal Saluja
Bonus
Write a function that detects the periodicity of the LFSR configuration.
4
u/lukz 2 0 Jan 17 '18 edited Jan 17 '18
BASIC for Sharp MZ-800
Every beginner who wants to start using computers should learn BASIC, right? Well, ... if we go back to eighties, it's true. And we can relive those moments thanks to emulators of the early personal computers.
About the program:
The input format is slightly changed - use a semicolon instead of a comma. That is because the INPUT statement has a special handling of the comma character and would not normally return it as a part of a string.
Lines 10-12 parse the input to get the taps, the flag if XNOR should be used, initial seed and the number of clock steps. Lines 14 and 18 form the main loop of the requested number of clock steps. Line 16 computes new input bit, line 17 updates the state.
Emulator screenshot.
Source:
3
u/skeeto -9 8 Jan 17 '18
C. Half of the work is just parsing and printing. Supports up to a 64-bit state since it actually stores the LSFR state in an integer (in reverse bit order) as it operates.
3
u/[deleted] Jan 17 '18
+/u/CompileBot Ruby
3
u/thestoicattack Jan 17 '18 edited Jan 18 '18
C++17. The input seemed like a pain, and I wanted to do some templating fun, so this assumes we know what LFSR we want to implement at compiler time. A declaration like
LFSR<3, Xor, 0, 2> f;will give a function object f that works like the first example. I desperately wanted to use another fold expression in the implementation, likeauto result = ((static_cast<bool>((1 << Taps) & in) ^ ...);-- but that makes g++ warn for more parentheses. Also, there's no builtin operator for xnor, so we can't fold that either. Instead, we work around with an array and accumulate. In practice, that all goes away, like for example:Another simplification: we run forever. If you want just the first n, it's your responsibility to pipe the output to
head. Compiler explorer: https://godbolt.org/g/MzzZow2
u/popillol Jan 17 '18
Go / Golang Playground Link. I think I did this correctly (hand checked calculations), but am getting a different order in the output stream. Which I think is OK due to this (from the wiki)
Output (for challenge inputs)
2
u/chunes 1 2 Jan 18 '18
Factor
Output:
3
u/[deleted] Jan 17 '18
It looks like the tap indices given are actually counted with 0 starting at the left and increasing to the right. Some implementations may use a raw integer for storing the bits, which is typically read and written as MSB first. In such implementations, index zero is LSB (the digit on the right). This wasn't made clear in the original question, so make sure your tap indices are adjusted properly!
1
u/[deleted] Jan 17 '18 edited Jan 17 '18
Rust - I've consistently found elegant solutions for programming problems in Rust. Its standard library is solid, packed with features without making it feel bloated. It's my new favorite language for general use.
Output:
EDIT: Fixed error message and WE REQUIRE ADDITIONAL ITERATORS
EDIT 2: Forgot to add output
1
u/Gprime5 Jan 17 '18 edited Jan 17 '18
Python 3.5
Isn't the periodicity just 2bit_width - 1 ?
Challenge solutions:
1
u/vault_dweller_707 Jan 17 '18
Python 3.6.3
This is my first attempt at submitting a solution, so I apologize if I’ve done something incorrectly. I’d love some feedback as I am a Python noob.
Output:
1
u/OldNewbee Jan 17 '18 edited Jan 18 '18
PYTHON 3.6
Output:
1
u/zatoichi49 Jan 17 '18 edited Jan 17 '18
Method:
Write a reduce function for XOR and XNOR, and apply that to a list of the bits at each tap value. Add this result to the left hand side (most significant bit) and remove one bit from the right hand side (least significant bit). Return the new register, and repeat for the total steps required.
Python 3:
Output:
1
u/KiotiKid Jan 18 '18
Java
First time submitting something so hopefully I have done this correctly!
Edit: still getting the hang of markdown..... sigh
Source
Output
1
u/DEN0MINAT0R Jan 18 '18 edited Jan 18 '18
Python 3
Output for Inputs 2 & 3:
2:
3:
1
u/g00glen00b Jan 18 '18 edited Jan 18 '18
Java 8: I could probbably have made it shorter by directly logging the output rather than putting it all in a stream, but other than that I'm happy with the result.
Testing it out:
The results:
1
u/[deleted] Jan 18 '18
Haskell
A simple int array to store the bit array.
1
u/glenbolake 2 0 Jan 18 '18 edited Jan 18 '18
The obvious way to do it in Python would be to do a lot with strings and comprehensions.
So obviously I used bitwise operators.
+/u/CompileBot Python3
1
u/octolanceae Jan 18 '18
C++11
Output:
1
u/Specter_Terrasbane Jan 18 '18
Python 2
1
u/Minolwa Jan 18 '18
Scala
Output of final challenge
1
u/mcbears Jan 18 '18
J: most of the work is parsing/printing, the real action is in the third-to-last line
1
u/dsfirebreath Jan 19 '18
Go. Playground Link. Any feedback is welcome.
Source:
Output:
1
u/chrissou Jan 19 '18
Clojure
Results:
1
u/[deleted] Jan 20 '18
F# This will run directly in FSI.exe. This was a bit interesting because interacting with bits like this isn't supported in F# as easily as it is in lower-level languages like C. So, the solution is not necessarily elegant or fast, but it does the job.
Output:
1
u/Satoshi_Hodler Jan 20 '18 edited Jan 20 '18
JavaScript
I'm a noob, hope my code is not too ugly.
Results
1
u/FrankRuben27 0 1 Jan 21 '18
Using Ada. Very late to the party, but I had to take the Snobol/Spitbol detour for the parsing part - and it was absolutely worth it. Ada means a lot of typing, nevertheless the language grows on me...
1
u/do_hickey Jan 22 '18
PYTHON 3.6
I have no real formal coding experience or education, so any feedback is appreciated. I think I took about half the time trying to get the regex stuff right, and another good chunk of my time trying to figure out how to cascade feedback functions through more than 2 tap positions.
Sorry it's a bit stylistically garbage, but I'm just happy it worked!
Source:
Output:
1
u/juanchi35 Jan 28 '18
Javascript
1
u/TSLRed Feb 08 '18
Python
TIL the magic of lambdas and reduce
1
u/altorelievo Feb 10 '18
Python 3.6
evalis called on the cmd line input...needless to say run with caution.