Linear-feedback shift register's taps in c++

329 Views Asked by At

I have a bit more specific question than before (maybe in old thread someone give more general answer) about using the "tap" on Linear-feedback shift in c++. I have scramler state of x7+x6+x2, so i need to XOR bytes number 7, 6, and 2, and then add result to the begin of next shift. The initial state is 79.

int main() {
    int a,b = 79;
    cout << " b = " << bitset<7>(b) << std::endl;
    int newbyte;
    for (int i = 1; i<11; i++){

//here i creating new bytes using XOR but, i have no idea what happen here
        newbyte = ((((a >> 6) ^ (a >>5) ^ (a >>1) ^ a ) & 0x01 ) << 6 ) | (a >> 1);
        a = (a >> 1) | (newbyte << 6);

        cout << "Iteration " << i << " of a = " << bitset<7>(a) << std::endl;}

}

My problem is i cant figure out how to get the x7+x6+x2 into c++ code and then add it to the next iteration. I tried just copy part of the code from wiki, but it gives me wrong result.

What it supposed to give out:

1 0 0 1 1 1 1
0 1 0 0 1 1 1
1 0 1 0 0 1 1
1 1 0 1 0 0 1
1 1 1 0 1 0 0
0 1 1 1 0 1 0
0 0 1 1 1 0 1
1 0 0 1 1 1 0
0 1 0 0 1 1 1
1 0 1 0 0 1 1

What im getting:

Iteration 1 of a = 0000000
Iteration 2 of a = 0000000
Iteration 3 of a = 0000000
Iteration 4 of a = 0000000
Iteration 5 of a = 0000000
Iteration 6 of a = 0000000
Iteration 7 of a = 1000000
Iteration 8 of a = 0100000
Iteration 9 of a = 0010000
Iteration 10 of a = 0001000

Can someone explain, please, how exactly shifting bytes to left or right helping me with XOR-ing specific "taps"?

Like lets say we have sequence 1001, and we want to scramble it by x3+x2. If i shift 1001 by 3 and 2 bytes to the right we will get 0001, and 0010. If i XOR them i will get 1100, right? But why it is so? I been thinking that meaning of x3+x2 is take 3 and 2 bytes and XOR them, so it would be more like that: Byte 3 = 0, byte 2 = 0, result equal 1, is it right? Or do i got it all completely wrong?

0

There are 0 best solutions below