Advertisement
Mohammed_Ahmed

LFSR

Apr 1st, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <bitset>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. // int x, y;
  9.  bitset <6> inpSeq;
  10.  bitset <6> polyPos;
  11.  bitset <6> operSeq;
  12.  bitset <6> bit;
  13.  vector <int> xorArray;
  14.  vector <int> keyReg;
  15.  cout << "Enter a 6-bit sequence: \n";
  16.  cin >> inpSeq;
  17.  cout << "Enter poly: \n";
  18.  cin >> polyPos;
  19.  for ( unsigned int i = 0;  i < polyPos.size(); i++)
  20.  {
  21.   if ( polyPos[i] == 1)
  22.   {
  23.    xorArray.push_back(i);
  24.   }
  25.  }
  26.  cout << "----" << "\n";
  27.  operSeq = inpSeq;
  28.  keyReg.push_back(inpSeq[0]);
  29.   int x = xorArray[0];
  30.   bit[5] = operSeq[x];
  31.   do {
  32.   for ( unsigned int j = 1; j < xorArray.size() ; j=j+1 )
  33.   {
  34.   bit[5] = bit[5] ^ operSeq[j];
  35.   }
  36.   operSeq >>= 1;
  37.   operSeq[5]  = bit[5];
  38.   cout << operSeq << "\n";
  39.   keyReg.push_back(operSeq[0]);
  40.  }
  41.  while (operSeq != inpSeq);
  42.  cout << "Generated key is: ";
  43.  for (unsigned int k = 0; k < keyReg.size(); k++)
  44.   {
  45.   cout  <<  keyReg[k];
  46.   }
  47.  cout << "\n";
  48.  cout << "Bit 1 positions: ";
  49.  for ( unsigned int g = 0; g < xorArray.size(); g++)
  50.  {
  51.   cout << xorArray[g];
  52.  }
  53.  cout << "\n";
  54.  cout << "Key length is: " << keyReg.size();
  55.  cout << "\n";
  56.  cin.get();
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement