An efficient linear feedback shift register (LFSR) class written in C
Copy LFSR.c and LFSR.h to your project and import the library :
#include "LFSR.h"
To create an LFSR : (by default all bits are set to 0. Here a 15 bits LFSR is created)
LFSR lfsr(15);
To initialize the LFSR, the first bit is set to 1
lfsr.setBit(0, true);
To perform a right shift with X0 xor X1 as feedback (X15 X14 1 in polynomial notation)
lfsr.rightShift(lfsr.getFirstBit() xor lfsr.getBit(1) xor lfsr.getBit(4));
Display first 8-bit of the LFSR :
lfsr.get8bit()
Get the size in bit of the register :
uint32_t getSize();
Get the raw value of the
uint32_t getArrayElement(uint32_t bitPosition);
Get the size of internal 32-bit array used for the register
uint32_t getArraySize();
Perform a right shift
void rightShift(bool last);
Perform a left shift
void leftShift(bool first);
Get first 8-bit
uint8_t get8bit();
Get first 16-bit
uint16_t get16bit();
Get first 32-bit
uint32_t get32bit();
Get the value at a specific position in the internal 32-bit array used for the register
uint32_t get32bitArray(uint32_t position);
Get a specific bit :
bool getBit(uint32_t bitPosition);
Get the first bit (equivalent to getBit(0) :
bool getFirstBit();
Get the last bit :
bool getLastBit();
Set a specific bit
void setBit(uint32_t bitPosition, bool value);
Set the first bit (equivalent to setBit(0, value))
void setFirstBit(bool value);
Set the last bit
void setLastBit(bool value);
Save the content of the register to a buffer
void save(uint32_t * &output);
Compare the register to a saved register
bool compare(uint32_t * &output);
Restaure the register with a saved register
void set(uint32_t * &output);
The example calculates the length cycle of a 15 bits register with X15 X14 1 as feedback
To build the sample :
g -std=c 11 LFSR.cpp main.cpp -o lfsr
Output of the sample is :
counter = 32767
Samples folder contains some use cases like A5/1 encryption