Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- // Helper method to get the bit of val at a certain position n
- int get_bit(uint16_t *val, int n) {
- return (*val >> n) & 1;
- }
- void lfsr_calculate(uint16_t *reg) {
- /* YOUR CODE HERE */
- // Declarations
- int bit;
- // Set the dummy bit equal to XOR of the bits at 16, 14, 13 and 11
- bit = get_bit(reg, 0) ^ get_bit(reg, 2) ^ get_bit(reg, 3) ^ get_bit(reg, 5);
- // Right shift the register by 1
- *reg = *reg >> 1;
- // Set the most significant bit of the register to the dummy bit
- *reg = (*reg & ~(1 << 15)) | (bit << 15);
- }
- int main() {
- int8_t *numbers = (int8_t*) malloc(sizeof(int8_t) * 65535);
- if (numbers == NULL) {
- printf("Memory allocation failed!");
- exit(1);
- }
- memset(numbers, 0, sizeof(int8_t) * 65535);
- uint16_t reg = 0x1;
- uint32_t count = 0;
- int i;
- do {
- count++;
- numbers[reg] = 1;
- if (count < 24) {
- printf("My number is: %u\n", reg);
- } else if (count == 24) {
- printf(" ... etc etc ... \n");
- }
- for (i = 0; i < 32; i++)
- lfsr_calculate(®);
- } while (numbers[reg] != 1);
- printf("Got %u numbers before cycling!\n", count);
- if (count == 65535) {
- printf("Congratulations! It works!\n");
- } else {
- printf("Did I miss something?\n");
- }
- free(numbers);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement