A
A
arcuda2016-01-28 09:19:30
Do it yourself
arcuda, 2016-01-28 09:19:30

Linear feedback shift register in C#?

Please help with RSLOS.
I'm trying to write an algorithm based on a wiki article.
Here is my code:

int S = 0x00000001;
        private void RandomLFSR (object sender, RoutedEventArgs e)
        {            
            S = ((((S >> 31) ^ (S >> 30) ^ (S >> 29) ^ 
                (S >> 27) ^ (S >> 25) ^ S )) 
                    & 0x00000001 ) << 31 | S >> 1;
            output.Text += S.ToString();             
        }

The result is the following sequence:

-2147483648-1073741824-536870912-268435456-134217728-67108864-33554432-16777216-8388608-4194304-2097152-1048576-524288-262144-131072-65536-32768-16384-8192-4096-2048-1024-512-256-128 -64-32-16-8-4-2-1-1-1-1-1-1-1-1

Where is the mistake? Help me please :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Zubkov, 2016-01-28
@arcuda

in general, it is better to look at the output data in binary representation, then it will be clearer
at random I will assume that this is the case:
x >> y - bit shift to the right. If the left operand is int or long, then the left bits are filled with the sign bit. If the left operand is uint or ulong, then the left bits are filled with zero.
i.e. try unsigned type

V
Vladimir Martyanov, 2016-01-28
@vilgeforce

The error, as always, is that the debugger is not being used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question