wrong answer

I have code like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>

unsigned LFSR2(unsigned lfsr2, unsigned start_state2, unsigned bit2) {
    unsigned period = 0;

    do {
        bit2 = (((bit2 >> 31) ^ (bit2 >> 30) ^ (bit2 >> 29) ^ (bit2 >> 27) ^ (bit2 >> 25) ^ bit2 ) & 1 );
        lfsr2 = (lfsr2 >> 1) | (bit2 << 31);
        std::cout << std::bitset<8>(lfsr2) << std::endl;

        ++period;
    } while (lfsr2 != start_state2);

    return period;
}

int main() {
    unsigned bit2;
    unsigned start_state2 = 0b11111111;
    unsigned lfsr2 = start_state2;
    LFSR2(lfsr2, start_state2, bit2);
}


and i have some result:

01111111
00111111
00011111
00001111
00000111
00000011
00000001
00000000
00000000
00000000
00000000
00000000
...
...


but this result is wrong. As I understand it should works not like that. Why do I get this result?
Last edited on
GIGO. bit2 is garbage value, never assigned.
@jonnin but when I write:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>

unsigned LFSR(unsigned lfsr, unsigned start_state, unsigned bit) {
    unsigned period = 0;

    do {
        bit = (lfsr ^ (lfsr >> 1)) & 1;
        lfsr = (lfsr >> 1) | (bit << 3);
        std::cout << std::bitset<4>(lfsr) << std::endl;

        ++period;
    } while (lfsr != start_state);
    return period;
}

int main() {
    unsigned bit;
    unsigned start_state = 0b1001;
    unsigned lfsr = start_state;
    LFSR(lfsr, start_state, bit);
}


I have true result:

1100
0110
1011
0101
1010
1101
1110
1111
0111
0011
0001
1000
0100
0010
1001
Last edited on
In your latest code, you are not using the value of 'bit' uninitialized.

Before, in the first iteration of the loop:
bit2 = (((uninitialized >> 31) ^ (uninitialized >> 30) ^ (uninitialized >> 29) ^ (uninitialized >> 27) ^ (uninitialized >> 25) ^ uninitialized ) & 1 );
After:
bit = (lfsr ^ (lfsr >> 1)) & 1;

In your latest code, you don't need bit to be a parameter to the function, because you overwrite the value on line 7.
Last edited on
This might help understand the problem...
https://www.youtube.com/watch?v=Ks1pw1X22y4

Basically your tap bits end up filling bit2 with 0s, when it is zero you have nowhere to go.

Edit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <bitset>

int main()
{
    uint32_t state = 0b0000'0000'0000'0000'0000'0000'1111'1111;
    uint32_t start_state{ state };
    uint32_t period{ 0 };
    uint32_t tap_bit{ 0 };
    do
    {
        tap_bit = ((state >> 31) ^ (state >> 30) ^ (state >> 29) ^ (state >> 25) ^ (state >> 0));
        state = (state >> 1) | (tap_bit << 31);
        
        std::cout << std::bitset<32>(state) <<'\n';
        period++;

    } while ((period != 0) && (start_state != state));

    std::cout << "Period: " << period << '\n';

} 

it takes a few iterations to get the string of 0s bit shifted out the way...
10000000000000000000000001111111
01000000000000000000000000111111
00100000000000000000000000011111
00010000000000000000000000001111
10001000000000000000000000000111
01000100000000000000000000000011
00100010000000000000000000000001
10010001000000000000000000000000
11001000100000000000000000000000
01100100010000000000000000000000
00110010001000000000000000000000
00011001000100000000000000000000
00001100100010000000000000000000
00000110010001000000000000000000
10000011001000100000000000000000
01000001100100010000000000000000
10100000110010001000000000000000
01010000011001000100000000000000
10101000001100100010000000000000
01010100000110010001000000000000
10101010000011001000100000000000
11010101000001100100010000000000
01101010100000110010001000000000
10110101010000011001000100000000
01011010101000001100100010000000
00101101010100000110010001000000
10010110101010000011001000100000
01001011010101000001100100010000
00100101101010100000110010001000
10010010110101010000011001000100
01001001011010101000001100100010
10100100101101010100000110010001

...
00010011010010010010111010110001
00001001101001001001011101011000
00000100110100100100101110101100
00000010011010010010010111010110
10000001001101001001001011101011
01000000100110100100100101110101
00100000010011010010010010111010
10010000001001101001001001011101
01001000000100110100100100101110
10100100000010011010010010010111
11010010000001001101001001001011
01101001000000100110100100100101
10110100100000010011010010010010
01011010010000001001101001001001
10101101001000000100110100100100
01010110100100000010011010010010
00101011010010000001001101001001
10010101101001000000100110100100
11001010110100100000010011010010
11100101011010010000001001101001
01110010101101001000000100110100
10111001010110100100000010011010
01011100101011010010000001001101
Last edited on
@The Gray Wolf Thanks!
Registered users can post here. Sign in or register to post.