如何在c ++中为不同的多项式创建LFSR

时间:2015-07-24 16:23:48

标签: c++ bit-manipulation bit-shift

我试图了解right >>left <<如何在c ++中转换操作。我在网上阅读了一些文章和一些主题,但我仍然感到困惑。 我试图根据用户输入来编码 LFSR(线性反馈移位寄存器),该用户输入应该给出长度,种子和多项式抽头位置作为LFSR的输入代码。

代码如下:

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
    string seed;
    unsigned int length, pos;

    cout << "Type the length and the seed" << endl;
    cin >> length >> seed;
    cout << "Polynomial tap positions" << endl;
    cin >> pos;

    //Creating array with the LFSR size
    unsigned int *sizee = new unsigned int[length];
    //Convert the seed from char to int
    for (unsigned int i = 0; i < length; i++) {
        sizee[i] = seed[i] - '0';
    }
    //Shifting
    unsigned int seq = std::pow(2,length)-1;
    for (unsigned int i = 1; i <= seq ; i++) {
        //Shift Operation here
        //Show user the value
    }

    delete[] sizee;

    return 0;
}

如何将位移位,例如,长度为5的LFSR中的种子00001和右侧的位置(X位置)5和3(x ^ 5 + x ^ 3 + 1)?我希望得到类似的东西:00001&gt; 10000&gt; 01000&gt; 00100> 10010,依此类推,直到循环结束,将Fibonacci视为架构类型。

2 个答案:

答案 0 :(得分:0)

首先,您将无法使用&gt;&gt;和&lt;&lt;运算符,因为你有一组int。

&lt;&lt;和&gt;&gt;运算符仅为数值数据类型定义。你应该切换到只使用一个int。

使用单个int,LFSR的算法变为:

Calculate your tap bit: tapbit = necessary bits XORed ( ^ ) together
// ^ Note this will require shifts as well, I'd reccomend a for loop through all your taps

Shift register to the right: seed = seed >> 1;
Set the leftmost bit to the calculated tap bit: seed = seed | ( tapbit << length );

答案 1 :(得分:0)

如果要在计算机上以整数编码LFSR,则需要首先了解LFSR和整数使用的表示。有两个重要的问题/差异:

  • LFSR通常将其位从1开始编号,点击位 i 对应于多项式中 x i
  • 整数通常将它们的位编号从0开始,位 i 对应的值 2 i
  • LFSR传统上在左侧显示位1,在右侧显示最高位
  • 整数通常用big-endian形式写,右边的位0和左边的最高位

当您为LFSR使用整数时,这些会导致两个重要的事情:

    LFSR的
  • i 变为整数位 i-1
  • LFSR的右移变为整数的左移。

所以你的基本LFSR步骤变为:

seed = (seed << 1) | parity(seed | polynomial)

其中seed是LFSR的内容(加上之前当你的整数大小大于你的LFSR长度时移出的额外位),polynomial是抽头位 - 一个位为<的整数< em> i-1 为polynoimal中的每个 x i 设置,parity是一个计算所有位的xor的函数一个整数 - 可以在大多数CPU上使用标记技巧或单个指令来完成,但是没有简单的方法可以在C中表达它。