【问题标题】:A variable length bitset in C++ [duplicate]C ++中的可变长度位集[重复]
【发布时间】:2014-04-07 14:36:03
【问题描述】:

我有一个工作的 4 位线性反馈移位寄存器,使用 3 个长度为 4 的位集:inpSeq、operSeq 和位。我想让程序接受一个可变长度的位序列,所以那些以前的位集应该是可变长度的。用户可以输入 inpSeq 的序列,并且程序将三个位集设置为与用户提供的序列具有相同的长度。关于如何实现这一目标的任何想法?如果我可以询问示例代码!

代码如下:

#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();
}

【问题讨论】:

  • 如果您希望在运行时确定大小,则需要使用 vector&lt;bool&gt; 而不是 bitset
  • @PaulR 谢谢!我想我可以对这个 bool 向量的内容进行 XOR 之类的逻辑运算,不是吗?而且打印的时候会是1和0的形式还是真假?
  • 可以使用 boost::dynamic_bitset 类:boost.org/doc/libs/1_55_0/libs/dynamic_bitset/…

标签: c++ bitset variable-length


【解决方案1】:

std::vectorstd::vector&lt;bool&gt; 有一个optimization,并且可以在运行时设置向量的大小。

【讨论】:

  • 如果你打算使用vector请先阅读这个:isocpp.org/blog/2012/11/on-vectorbool
  • @Michael 我使用operSeq &gt;&gt;= 1 来右移bitset operSeq 的位。布尔向量是否有等效方法?
  • @MohamedAhmed 如果向量的构建方式是最低有效位 (LSB) 位于向量的顶部(最后推送),那么您只需要做的就是右移,是调用pop_back。
猜你喜欢
  • 2013-06-23
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 2013-01-04
  • 2012-10-25
相关资源
最近更新 更多