Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 线性反馈移位寄存器效率_C#_Bit Manipulation_Shift Register - Fatal编程技术网

C# 线性反馈移位寄存器效率

C# 线性反馈移位寄存器效率,c#,bit-manipulation,shift-register,C#,Bit Manipulation,Shift Register,我有以下代码实现线性反馈移位寄存器的移位操作: public int DoShift() { //Find new top bit int feedback = Contents & tapSequence; int newBit = 0; for(int i = 1; i <= length; i++) { newBit = 1 & (newBit ^ feedback); feedback >

我有以下代码实现线性反馈移位寄存器的移位操作:

public int DoShift()
{
    //Find new top bit
    int feedback = Contents & tapSequence;
    int newBit = 0;
    for(int i = 1; i <= length; i++)
    {
        newBit = 1 & (newBit ^ feedback);
        feedback >>= 1;
    }
    //Remember falloff, shift register, add new bit
    int result = Contents & 1;
    Contents >>= 1;
    Contents += newBit << (length - 1);
    return result;
}
public int DoShift()
{
//查找新的顶级位
int反馈=内容和点击顺序;
int-newBit=0;
对于(inti=1;i>=1;
}
//记住衰减、移位寄存器、添加新位
int result=Contents&1;
内容>>=1;
Contents+=newBit试试这个:

public int DoShift()
{
    int newBit = 1 << (length - 1); // you can save it as class member
    int result = Contents & 1;
    int feedback = Contents & tapSequence;
    Contents >>= 1;
    while(feedback != 0) {
      feedback &= feedback - 1;
      Contents ^= newBit;
    }
    return result;
}
public int DoShift()
{
int newBit=1>=1;
while(反馈!=0){
反馈&=反馈-1;
Contents^=newBit;
}
返回结果;
}
此外,还有一种更有效的方法,叫做“反向LSFR”。它的想法是,若结果为1,只需将tapSequence应用于整个寄存器一次


参见示例:

已采用以下解决方案:

public int DoShift()
{
    //Remember falloff, shift register, add new bit
    int result = Contents & 1;
    Contents = (Contents >> 1) ^ 
        ((CountBits(Contents & tapSequence) % 2) << (length - 1));
    return result;
}

//Brian Kernighan method of counting bits
public static int CountBits(int value)
{
    int count = 0;
    while (value != 0)
    {
        count++;
        value &= value - 1;
    }
    return count;
}
public int DoShift()
{
//记住衰减、移位寄存器、添加新位
int result=Contents&1;
目录=(目录>>1)^

((计数位(内容和tapSequence)%2)你真的需要有符号的数字吗?也许右移的符号扩展导致了比预期更多的点击率?你做了发布编译吗?在调试器外运行它吗?你能早点打破
for
循环吗?在
循环时,以某种方式将其更改为
newBit
基本上是
反馈的奇偶校验。看一看,找到一些循环的替代方案。顺便说一句,最后一个
内容+=
可以表示为按位,或者因为
newBit
只有一位。信号号?我假设你参考了tap序列…想不出更好的实现方法。没有发布编译,只需使用VS调试工具运行它。我想它可以一旦反馈=0,uld就会被破坏,我会补充这一点。为什么改成while循环会有帮助呢?没有想到它会像奇偶校验一样,感谢链接,会有一个挖掘。会不会
Contents=Contents^(newBit)可能。但你必须测量一个明确的答案。顺便说一句,我会将位安排在相反的方向(因此,较低指数的系数对应较低的有效位)。但这只是口味的问题。