[英]Converting the bits of a vector to a decimal integer
我正在嘗試將向量的位轉換為十進制整數。 我的程序是一個可變線性反饋移位寄存器。 首先,它詢問用戶LFSR初始序列的長度,然后詢問序列本身以及要進行異或的位的位置。 因此,如果我為序列的長度輸入4,為位序列輸入1110,為多項式輸入20,則密鑰為0111100,它存儲在向量keyReg中,我嘗試使用for條件將其轉換為十進制數:
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();
}
我想你的意思是:
for ( unsigned int i = 0; i < keyReg.size(); i++)
{
if (keyReg[i]==1)
{
key = key+(1 << i); // this is 2^i
cout << key << "\n";
}
}
^
是XOR的按位運算符,因此從編譯器的角度來看,該代碼“有效”。
工作原理:
我找不到相關的問題,但在其他地方解釋了“ (1 << i)
”。 1
被視為整數。 然后整數上的operator<<
是按位左移( i
位置)。
因此它使000001
並將其向左移動,例如,當i
為3時,它將產生001000
。 有效產生2^i
整數。
當然,可以使用更明確的內容,但是std :: pow只為浮點類型定義,因此需要使用一些轉換。
(1 << i)
也帶來一些安全隱患。 您需要注意用於移位的值的類型(其大小),並且用於移位,寫入(1<<128)
可能會產生一些意外的結果。 無論如何,對於大多數情況,這是獲得2^i
的最佳方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.