Question

I am trying to convert the bits of a vector into a decimal integer. My program is a variable linear feedback shift register. At first it asks the user for the length of the initial sequence of the LFSR, then it asks for the sequence itself and the position of the bits to be xored. So if I entered 4 for the length of the sequence, 1110 for the bit sequence and 20 for polynomial, the key is 0111100, it is stored in a vector keyReg, I tried converting it into a decimal number by using a for condition:

for ( unsigned int i = 0; i < keyReg.size(); i++)
 {
  if (keyReg[i]==1)
   {
    key = key+(2^i);
    cout << key << "\n";
   }
 }

But that is not producing the correct decimal equivalent to 0111100. What to do? Here is the full program:

#include <iostream>  //Standard library.
#include <boost/dynamic_bitset.hpp>    //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;
 int xx = 0;
 int polyLoc;
 int key = 0;
 boost::dynamic_bitset<> inpSeq(5);
 boost::dynamic_bitset<> operSeq(5);
 boost::dynamic_bitset<> bit(5);
 vector <int> xorArray;
 vector <int> keyReg;
 cout << "What is the legnth of the sequence?";
 cin >> xx;
 inpSeq.resize(xx);
 operSeq.resize(xx);
 bit.resize(xx);
 cout << "Enter a bit sequence: \n";
 cin >> inpSeq;
 int seq_end = inpSeq.size() - 1;
 cout << "Enter polynomial:";
 cin >> polyLoc;
 while(polyLoc>0)
 {
  xorArray.push_back(polyLoc%10);
  polyLoc/=10;
 }
 sort(xorArray.rbegin(), xorArray.rend());
 cout << "\n";
 operSeq = inpSeq;
 keyReg.push_back(inpSeq[0]);
  int x = xorArray[0];
  do {
  for (unsigned int r = 1; r < xorArray.size(); r++)
  {
  bit[seq_end] = operSeq[x];
  y = xorArray[r];
  bit[seq_end] = bit[seq_end] ^ operSeq[y];
  }
  operSeq >>= 1;
  operSeq[seq_end]  = bit[seq_end];
  keyReg.push_back(operSeq[0]);
  turnCount ++;
  cout << operSeq << "\n";
 }
 while ((operSeq != inpSeq) && (turnCount < 1024));
 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";
    }
   }
for ( unsigned int i = 0; i < keyReg.size(); i++)
 {
  if (keyReg[i]==1)
   {
    key = key+(2^i);
    cout << key << "\n";
   }
 }
cout << "Key is " << key << "\n"; 
cin.get();
}
Was it helpful?

Solution

I think you meant:

for ( unsigned int i = 0; i < keyReg.size(); i++)
 {
  if (keyReg[i]==1)
   {
    key = key+(1 << i); // this is 2^i
    cout << key << "\n";
   }
 }

^ is a bitwise operator for XOR so the code was "valid" from the compiler's point of view.

Why it works:

I cannot find a relevant question but "(1 << i)" was explained somewhere else. 1 is treated as an integer. Then operator<< on integer is a bitwise left shift (by i places).

So it makes 000001 and shifts it left, e.g. when i is 3 it produces 001000. Effectively producing 2^i integer.

Of course one could use something more explicit, however std::pow is defined only for floating point types, so one would need to use some conversions.

(1 << i) also poses some safety concerns. You need to take care of the type of the values you use for shifting (their size), and value you use for shifting, writing (1<<128) might give some unexpected results. Anyway it is the best way to get 2^i for most cases IMO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top