Skip to content

Instantly share code, notes, and snippets.

@LBijaminas
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save LBijaminas/9163441 to your computer and use it in GitHub Desktop.

Select an option

Save LBijaminas/9163441 to your computer and use it in GitHub Desktop.
20-bit LFSR with a verification function to verify that it does not repeat after the period
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_PERIOD 1048575
void generateNums(int *);
void checkDups(int *);
int main(){
int genNums[MAX_PERIOD];
generateNums(genNums);
checkDups(genNums);
return 0;
}
/*
* Generate the numbers using LFSR, & puts
* it into genNums array
*/
void generateNums(int *array){
int lfsr = 0b01110010011010100011;
int final = lfsr;
int mask = 0b11001010000000000000;
FILE *file = fopen("text", "w+");
do {
int unsigned lsb = lfsr & 1; // least significant bit
lfsr >>=1; //shift right
/*
* toggle masks unneeded if lsb is 0
*/
if (lsb == 1)
lfsr ^= mask; //apply the mask
*array++ = lfsr;
fprintf(file, "%d \n", *(array - 1));
}while (lfsr != final); // runs MAX_PERIOD times
}
void checkDups(int *array){
for (int i = 0; i < sizeof(array)/sizeof(array[0]); i++){
// check the sign of array[abs(array[i])]
if(array[abs(array[i])] > 0){
// if its positive, make it negative
array[abs(array[i])] = -array[abs(array[i])];
} else {
// we found a repetition
printf("%d \n", array[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment