我试图将向量的位转换为十进制整数。我的程序是一个可变线性反馈移位寄存器。首先,它询问用户LFSR的初始序列的长度,然后它要求序列本身和要被xored的位的位置。因此,如果我为序列的长度输入4,为比特序列输入1110,为多项式输入20,则键为0111100,它存储在向量keyReg中,我尝试使用for条件将其转换为十进制数:< / p>
for ( unsigned int i = 0; i < keyReg.size(); i++)
{
if (keyReg[i]==1)
{
key = key+(2^i);
cout << key << "\n";
}
}
但是这并没有产生相当于0111100的正确小数。该怎么办? 这是完整的计划:
#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();
}
答案 0 :(得分:2)
我认为你的意思是:
for ( unsigned int i = 0; i < keyReg.size(); i++)
{
if (keyReg[i]==1)
{
key = key+(1 << i); // this is 2^i
cout << key << "\n";
}
}
^
是bitwise operator for XOR,所以代码是&#34;有效&#34;从编译器的角度来看。
为什么会这样:
我无法找到相关问题,但是&#34; (1 << i)
&#34;在其他地方解释过。 1
被视为整数。然后整数上的operator<<
是按位左移(i
个位置)。
因此它会000001
并将其向左移动,例如当i
为3时,它会生成001000
。有效地生成2^i
整数。
当然可以使用更明确的内容,但是std::pow仅针对浮点类型定义,因此需要使用一些转换。
(1 << i)
也引起了一些安全问题。您需要注意用于移位的值的类型(它们的大小)以及用于移位的值,写入(1<<128)
可能会产生一些意想不到的结果。无论如何,对于IMO的大多数情况来说,这是获得2^i
的最佳方式。