I am trying to understand how change the galois LFSR code to be able to specify the output bit number as a parameter for the function mentioned below. I mean I need to return not the last bit of LFSR as output bit, but any bit of the LFSR ( for example second or third bit). I am really stuck up with this question. Can anybody give some hint how to implement that?
#include < stdint.h >
uint16_t lfsr = 0xACE1u;
unsigned period = 0;
do {
unsigned lsb = lfsr & 1;
/* Get lsb (i.e., the output bit - here we take the last bit but i need to take any bit the number of which is specified as an input parameter). */
lfsr >>= 1;
/* Shift register */
if (lsb == 1)
/* Only apply toggle mask if output bit is 1. */
lfsr ^= 0xB400u;
/* Apply toggle mask, value has 1 at bits corresponding* to taps, 0 elsewhere. */
++period;
} while (lfsr != 0xACE1u);
If you need bit
k
(k = 0 ..15)
, you can do the following:This shifts the register
k
bit positions to the right and masks the least significant bit.