i need help with implementing the missing function outputing the rightmost bit o
ID: 3587651 • Letter: I
Question
i need help with implementing the missing function outputing the rightmost bit of the state ...
Your question has been answered
Let us know if you got a helpful answer. Rate this answer
Question: I need help with implementing the missing function outputing the rightmost bit of the state varia...
I need help with implementing the missing function outputing the rightmost bit of the state variable. The code is in C language.
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
/*The following struct will represent an LFSR with a 64 bit state. The taps are represented by a 64 bit number, where the ith bit (from the right) corresponds to p_i. The uint64_t is a 64 bit unsigned integer.
typedef struct {
uint64_t state; uint64_t taps;
} LFSR;
int parity(uint64_t N) { /* Return the parity of N*/
int p = __builtin_parity(N); // parity of first 32 bits
N = __builtin_bswap64(N); //move back 32 to the front
return (p+__builtin_parity(N))%2; //overall parity }
int read_lfsr(LFSR* L) {
/*Return the current output bit (the rightmost bit of the state variable) */
int x = L -> state &1;
return x;
}
void next_state(LFSR* L)
{
/*Takes LFSR.
Returns nothing.
Side effect: advances L to next state.(shift to the right and replace leftmost bit with appropriate value)
*/
/* You implement this.
Hint: make use of the parity() function provided above*/
}
Explanation / Answer
In computing, a linear-feedback shift register (LFSR) is a shift register whose input bit is a linear function of its previous state.
The `next_state` function updates the state of the LFSR according to the value of the tap bits. The simplest way to do this is to AND the state and taps variables, and then return the parity of the result. This works because AND is coordinate-wise multiplication modulo 2, and parity is a summation of bits modulo 2.
void next_state(LFSR* L)
{
/*Takes LFSR.
Returns nothing.
Side effect: advances L to next state.(shift to the right and replace leftmost bit with appropriate value)
*/
uint64_t res1= L->state &taps; //res1 will store coordinate-wise multiplication modulo 2,b/w state and taps variable
int answer=parity(res1)// parity is a summation of bits modulo 2
printf("%d ",answer);
}
Thanks !!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.