Re: LFSR in c




On Wed, 26 Sep 2006 dis_is_eagle@xxxxxxxxx wrote:

Hi.Thanks for the links. But can you explain it with a simple C
program?

(Explain the workings of a linear feedback shift register, you mean.
If you would say what you mean /in your post/, then we wouldn't have
to look backwards up the thread to find out what you're talking about,
and you might get more help. Certainly you'd be less annoying. Hint
hint.)

For example (this implements the same LFSR shown on the Wikipedia
article on LFSRs):
[UNTESTED CODE]

#include <stdio.h>

int LFSR(unsigned short *state, int (*f)(unsigned short))
{
int input = f(*state)? 1: 0;
int output = (*state & 0x8000)? 1: 0;
*state = (*state << 1) | input;
return output;
}

static int xorf(unsigned short val)
{
return ((val>>2) ^ (val>>3) ^ (val>>5)) & 1;
}

int main(void)
{
unsigned short state = 0x68F3;
printf("Output is %d\n", LFSR(&state, xorf));
printf("Output is %d\n", LFSR(&state, xorf));
printf("Output is %d\n", LFSR(&state, xorf));
printf("Output is %d\n", LFSR(&state, xorf));
printf("Output is %d\n", LFSR(&state, xorf));
return 0;
}

HTH,
-Arthur
.



Relevant Pages

  • Re: Deadlocks on Update
    ... hint if you are not updating the table. ... WITHhint. ... > avalId int 4 No ... CleanCnt:1 Mode: U Flags: 0x0 ...
    (microsoft.public.sqlserver.programming)
  • Re: New to c++.net (need help)
    ... > using namespace std; ... > int main ... Here's a hint for when ... your program is behaving other than how you expect, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Bit fiddling
    ... int n; should be unsigned int n;, or a better solution would have ... Hint #2: Is the value of ~3 even or odd? ... divisibility-by-three test would make a useful improvement. ... On the six-year-old machine sitting in front of me at the ...
    (comp.lang.c)
  • Re: Reading some pieces of a file
    ... ron wrote: ... find how to open and read a file (hint: ... read the answer and compare ... int main ...
    (alt.comp.lang.learn.c-cpp)
  • Re: convert decimal number in a hexadecimal number ?
    ... I just gave him a hint of solution.... ... My fuction char* itoa(int val, int base) is perfectly right ..it ... Are you sure 31 characters is enough for a 256-bit int on DS9K? ...
    (comp.lang.c)