3

I have a working 4 bit linear feedback shift register, using 3 bitsets of length 4: inpSeq, operSeq and bit. I want to make the program accept a variable length bit sequence, so those previous bitsets should be of variable length somehow. The user may enter a sequence ofr inpSeq and the program sets the three bitsets to be of the same length as that sequence provided by the user. Any ideas for how to achieve this? Sample code if I may ask!

Here is the code:

#include <iostream>  //Standard library.
#include <bitset>    //Library for 10 handling.
#include <vector>    //Variable size array.
#include <algorithm> //We use sorting from it.

using namespace std;

int main()
{
 int y = 0;
 int turnCount = 0;
 int count1 = 0, count0 = 0;
 bitset <4> inpSeq;
 int polyLoc;
 bitset <4> operSeq;
 bitset <4> bit;
 vector <int> xorArray;
 vector <int> keyReg;
 cout << "Enter a 4-bit sequence: \n";
 cin >> inpSeq;
 cout << "Enter polynomial:";
 cin >> polyLoc;
 while(polyLoc>0)
 {
  xorArray.push_back(polyLoc%10);
  polyLoc/=10;
 }
 cout << "xorArray is: ";
 for ( unsigned int i = 0; i < xorArray.size(); i++)
 {
  cout << xorArray[i] << " ";
 }
 sort(xorArray.rbegin(), xorArray.rend());
 cout << "\n";
 operSeq = inpSeq;
 keyReg.push_back(inpSeq[0]);
  int x = xorArray[0];
  cout << "x is: " << x << "\n";
  for ( unsigned int  i = 0; i < xorArray.size();  i++)
  {
   cout << xorArray[i] << "\n";
  }
  cout << "bit 3 of initial " << bit[3] << "\n";
  do {
  for (unsigned int r = 1; r < xorArray.size(); r++)
  {
  bit[3] = operSeq[x];
  cout << "bit 3 from prev: " << bit[3] << "\n";
  y = xorArray[r];
  cout << "opseq[y] is: " << operSeq[y] << "\n";
  bit[3] = bit[3] ^ operSeq[y];
  cout << "bit[3] after xor: " << bit[3] << "\n";
  }
  operSeq >>= 1;
  cout <<"operSeq after shift: " <<  operSeq << "\n";
  operSeq[3]  = bit[3];
  cout <<"opserSeq bit 4 after = bit[3]: " << operSeq[3] << "\n";
  cout <<"new operSeq: " << operSeq << "\n";
  keyReg.push_back(operSeq[0]);
  turnCount ++;
  cout << "--\n";
 }
 while ((operSeq != inpSeq) && (turnCount < 20));
 cout << "Generated key is: ";
 for (unsigned int k = 0; k < keyReg.size(); k++)
  {
  cout  <<  keyReg[k];
  }
 cout << "\n";
 cout << "Bit 1 positions: ";
 for ( unsigned int g = 0; g < xorArray.size(); g++)
 {
  cout << xorArray[g];
 }
 cout << "\n";
 cout << "Key length is: " << keyReg.size();
 cout << "\n";
 for ( unsigned int i = 0; i < keyReg.size(); i++)
 {
  if (keyReg[i]==1)
   {
    count1++;
   }
  else {
    count0++;
  }
 }
 cout << "Number of 0's: " << count0 << "\n";
 cout << "Number of 1's: " << count1 << "\n";
 if ( keyReg.size()%2 ==0)
  {
   cout << "key length is even. \n";
   if (count1==count0)
    {
   cout << "Key is perfect! \n";
    }
  else {
   cout << "Key is not perfect! \n";
    }
 }
  else
   {
  cout << "key length is odd. \n";
   if  ((count1==count0+1) || (count0==count1+1))
    {
   cout << "Key is perfect! \n";
    }
  else {
   cout << "Key is not perfect! \n";
    }
   }
  cin.get();
}
Mohamed Ahmed
  • 457
  • 1
  • 7
  • 25
  • 1
    You'll need to use `vector` instead of `bitset` if you want the size to be determined at run-time. – Paul R Apr 07 '14 at 14:38
  • @PaulR Thank you! I guess I can do logic operations like XOR on the contents of this bool vector, can't I? And when printing it, will it be in the form of 1 and 0 or true false? – Mohamed Ahmed Apr 07 '14 at 14:43
  • 2
    You can use the boost::dynamic_bitset class: http://www.boost.org/doc/libs/1_55_0/libs/dynamic_bitset/dynamic_bitset.html – PaulMcKenzie Apr 07 '14 at 14:58

1 Answers1

2

std::vector has an optimization for std::vector<bool>, and the size of a vector can be set at runtime.

Michael
  • 979
  • 6
  • 13
  • 3
    if you plan to use vector please first read this: http://isocpp.org/blog/2012/11/on-vectorbool – vlad_tepesch Apr 07 '14 at 14:42
  • @Michael I was using `operSeq >>= 1` to right shift the bits of bitset operSeq. Is there an equivalent method for a bolean vector? – Mohamed Ahmed Apr 07 '14 at 16:34
  • @MohamedAhmed if the vector is built in such a way that the least significant bit (LSB) is at the top of the vector (pushed last), all you have to do in order to shift right, is to call pop_back. – 4thex May 17 '21 at 10:24