1

I have the following vars:

char seed[NBITS + 1], x0[NBITS + 1], y0[NBITS + 1], z0[NBITS + 1], dT0[NBITS + 1];

And i want to change it values on this function:

void lfsr(char *bin, char *output)
{
    //bits significativos para fazer o xor NBITS -> NBITS,126,101,99;
    int bits[4];
    int bit;

    if(bin[0] == '0')
        bits[0] = 0;
    else if(bin[0] == '1')
        bits[0] = 1;

    if(bin[2] == '0')
        bits[1] = 0;
    else if(bin[2] == '1')
        bits[1] = 1;

    if(bin[21] == '0')
        bits[2] = 0;
    else if(bin[21] == '1')
        bits[2] = 1;

    if(bin[19] == '0')
        bits[3] = 0;
    else if(bin[19] == '1')
        bits[3] = 1;

    bit = bits[0] ^ bits[1] ^ bits[2] ^ bits[3] ^ 1;
    //reconstruir o vector de char depois do lfsr
    for(int i = 127; i >= 1; i--)
    {
        bin[i] = bin[i - 1];
    }

    bin[0] = (char)(48 + bit);  
    output = bin;
}

The way that I put the value in y0 from x is, for example, calling the lfsr functions like this:

lfsr(x0, y0);

What am I doing wrong?

I have to do 3 Fibonacci Linear Feedback Shift Register starting from x0.

x0 = 10101010101010
y0 = lfsr(101010101010)
z0 = lfsr(y0)
dT0 = lfsr(z0);

The results are good, but when I do the above code the value of x0 will be the same as dT0 if i use pointers. Can anyone help me?

Thanks. Cumps!

12
  • This is begging for a loop. Commented Dec 20, 2013 at 1:05
  • What's the point of this, output = bin;? Seems totally unnecessary. Commented Dec 20, 2013 at 1:06
  • thats the problem.. I don't know how to pass the bin value to variable y0.. I've tried with pointers, but then all variables that enter in that function will be the same.. Commented Dec 20, 2013 at 1:08
  • If you pass the address of an array to a function, the function has direct access to the array and can modify its contents. No need to pass it back. Commented Dec 20, 2013 at 1:09
  • 1
    Show code, and sample input, and the wrong output, and what do you expect from output, and what section you don't understand, finally the problem could be solve a lot faster. Commented Dec 20, 2013 at 1:19

1 Answer 1

3

Consider the following:

enter image description here

The numbers correspond to the taps. The bits are actually 15..0, left to right. The following is my implementation of the Fibonacci Linear Feedback Shift Register:

#include <stdio.h>

uint16_t fibLfsr(const uint16_t num)
{
    uint16_t tempNum;

    tempNum = (num) ^ (num >> 2) ^ (num >> 3) ^ (num >> 5);
    tempNum = (tempNum & 0x1) << 15;
    tempNum = (tempNum | (num >> 1));

    return tempNum;
}

int main(void)
{
    uint16_t testNum = 0xACE1;

    printf("%#X\n", testNum);
    testNum = fibLfsr(testNum);
    printf("%#X\n", testNum);

    return 0;
}

I'm not quite sure why you're using strings and converting them to binary. If this is necessary, you'll need some of the standard library APIs in stdlib and string to convert the string to an uint16_t before calling fibLfsr() and back to a string afterwards.

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer is correct. I'm working with 128bit binary number.. So i need to work with char vector. Thank you very much. Cumps!
@arturataide If the taps are the same, converting fibLfsr() to accept and return a 128-bit number should be pretty straight forward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.