Shift bit operations - LFSR

Hi, I'm trying to implement a LFSR (Linear Feedback Shift Register) in C++ using Shift bits operators (<< and >>). I've been reading articles to learn how to implement it but I'm really confused. My doubts are:

1. How can I shift the a value and do XOR operations between some bits and shift the XORed value to the first value?

for example:
1
2
3
  bit = 00001;
  //XORing the last two bits and shifting to the first one, we should have:
  bit == 10000;

I know to shift to right I can do bit >> 1, but what would be the type of bit variable? and how the make the first value be the XOR result?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <bitset>
int main()
{
    std::bitset<5> bitRegister(1);
    std::cout << bitRegister << std::endl;

    //get the value that will be put in the highest position after the shift
    bool valToShiftIn = (bitRegister[0] ^ bitRegister[1]);

    bitRegister >>= 1; //go ahead and shift the whole register over to the right

    //mock a 1 being shifted in using bitwise-OR
    // 1. take the result of the XOR
    // 2. shift it up 4 to get it in the proper place
    // 3. bitwise-OR it with the already shifted register to put the value in the proper position
    bitRegister |= ( (valToShiftIn) << 4);
    std::cout << bitRegister << std::endl;
}
Topic archived. No new replies allowed.