Skip to content

Instantly share code, notes, and snippets.

@Mahedi-61
Created August 10, 2018 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mahedi-61/6a3093a10979fccea782f56ac616f1c0 to your computer and use it in GitHub Desktop.
Save Mahedi-61/6a3093a10979fccea782f56ac616f1c0 to your computer and use it in GitHub Desktop.
c code for random bit generator
#include <stdio.h>
#include <stdint.h>
#include <string.h>
//#include <stdlib.h>
int main(int argc, char* argv[])
{
uint32_t lfsr = 0xACE1ACE1u;
unsigned period = 0;
int state;
//printf("Number of LFSR state : ");
//scanf("%d", &state);
char s[32 + 1];
int i, nb_input_bits, nb_buffer;
printf("Enter number of the input bits: ");
scanf("%d", &nb_input_bits);
nb_buffer = nb_input_bits / 32;
int buffer_count = 0;
int ignore = 0;
char *line = malloc(nb_buffer * 32 + 13); // 10+2+1
// file operation
int first_line = 1;
FILE *fp = fopen("binary.txt", "a");
do {
unsigned lsb = lfsr & 1; /* Get lsb (i.e., the output bit). */
lfsr >>= 1; /* Shift register */
if (lsb == 1) /* Only apply toggle mask if output bit is 1. */
lfsr ^= 0xB400B400u; /* Apply toggle mask, value has 1 at bits corresponding
/* to taps, 0 elsewhere. */
++buffer_count;
//seed = ACE1 = 1010110011100001, feedback polynomial = 1011010000000000
for (i = 0; i < 32; i++){
s[31 - i] = (lfsr & (1 << i)) ? '1' : '0';
}
// for nb_buffer = 1 value
if(nb_buffer == 1){
if (first_line == 1){
sprintf(line, "%d: %s", period, s);
}
else if(first_line > 1){
++period;
s[32] = '\0';
fprintf(fp, "%d: ", period);
fprintf(fp,"%s\n", s);
}
first_line++;
// for nb_buffer large value
}else if (nb_buffer > 1){
if(first_line != 1){
if(buffer_count == 1){
++period;
fprintf(fp, "%d: ", period);
fprintf(fp,"%s", s);
}else if (buffer_count == nb_buffer){
buffer_count = 0;
s[32] = '\0';
if (ignore != 0){
fprintf(fp,"%s\n", s);
}
ignore = 61;
}else{
if (ignore != 0)
fprintf(fp,"%s", s);
}
} //end of if loop
first_line++;
}//end of do loop
} while(lfsr != 0xACE1ACE1u);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment