简体   繁体   English

C++ do-while 循环中的两个条件

[英]Two conditions in a do-while loop in C++

I have been trying to make a linear feedback shift register, but there is something wrong in the code that I haven't been able to find, I tried to diagnose the problem by having the condition of the do-while loop that handles the main operation of the program, that is xoring the bits and shifting them, to be either one of two:我一直在尝试制作一个线性反馈移位寄存器,但是代码中有一些我无法找到的错误,我试图通过处理主要的 do-while 循环的条件来诊断问题程序的操作,即对位进行异或并将它们移位,使其成为以下两者之一:

 operSeq !=inpSeq 
 turnCount < 31. 

The program asks the user to enter 5 bits as the initial card of the LFSR, then he/she has to enter a second binary sequence representing the the polynomial: if it is x^5+x^4+x^2+x^1+1;程序要求用户输入 5 位作为 LFSR 的初始卡片,然后他/她必须输入表示多项式的第二个二进制序列:如果是x^5+x^4+x^2+x^1+1; the sequence that the user should enter is 10111 , these bits are examined if any of them is one, it's location will be stored in a vector named xorArray .用户应该输入的序列是10111 ,如果这些位中的任何一个是 1,则检查这些位,它的位置将存储在名为xorArray的向量中。 As a test case for this code, if the user enters 10101 for the initial card and 01111 for the poly location, the program will generate an indefinite number of binary sequences, those sequences do not follow what the xor operation correctly as it seems, and also they don't terminate after 31 turns as the do-while loop specifies!作为这段代码的一个测试用例,如果用户输入10101作为初始卡片和01111作为多边形位置,程序将生成无限数量的二进制序列,这些序列不会像看起来那样正确地遵循 xor 操作,并且它们也不会在 do-while 循环指定的 31 圈后终止! I need help diagnosing this code.我需要帮助诊断此代码。 Here is it:就这个:

#include <iostream>
#include <bitset>
#include <vector>
using namespace std;

int main()
{
    // int x, y;
    int turnCount = 0;
    bitset <5> inpSeq;
    bitset <5> polyLoc; 
    bitset <5> operSeq;
    bitset <5> bit;
    vector <int> xorArray;
    vector <int> keyReg;

    cout << "Enter a 5-bit sequence: \n";
    cin >> inpSeq;
    cout << "Enter poly: \n";
    cin >> polyLoc;

    for ( unsigned int i = 0;  i < polyLoc.size(); i++)
    {
       if( polyLoc[i] == 1)
       {
          xorArray.push_back(i);
       }
    }

    cout << "----" << "\n";
    operSeq = inpSeq;
    keyReg.push_back(inpSeq[0]);

    int x = xorArray[0];
    bit[4] = operSeq[x];
    do
    {
       for ( unsigned int j = 1; j < xorArray.size() ; j=j+1 )
       {
          bit[4] = bit[4] ^ operSeq[j];
       }

       operSeq >>= 1;
       operSeq[4]  = bit[4]; 
       cout << operSeq << "\n";
       keyReg.push_back(operSeq[0]);
       turnCount ++;
    } while ((operSeq != inpSeq) || (turnCount < 31));

    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";
    cin.get();
}

You should use the AND && operator and not the OR ||您应该使用 AND && 运算符而不是 OR || operator on your do {} while loop.您的 do {} while 循环上的运算符。 You want the loop to continue as long as both the conditions are true, not as long as one of them is true.只要两个条件都为真,您希望循环继续,而不是其中之一为真。

Your condition is (operSeq != inpSeq) || (turnCount < 31)你的条件是(operSeq != inpSeq) || (turnCount < 31) (operSeq != inpSeq) || (turnCount < 31) . (operSeq != inpSeq) || (turnCount < 31) So if either of the conditions is true, then it will continue to loop.因此,如果其中一个条件为真,则它将继续循环。 You want to stop the loop when turnCount >= 31 so your condition should be (operSeq != inpSeq) && (turnCount < 31) .您想在turnCount >= 31时停止循环,因此您的条件应该是(operSeq != inpSeq) && (turnCount < 31)

If your while loop isn't processing anything after turnCount reaches 31, then it's picking up (operSeq != inSeq).如果您的 while 循环在 turnCount 达到 31 后没有处理任何内容,则它正在接收 (operSeq != inSeq)。

In your while's ||在你的时候|| statement, if the first condition is true, it won't process the second condition at all.语句,如果第一个条件为真,则它根本不会处理第二个条件。 Try putting turnCount's condition first if (operSeq != inSeq) or && them together if they both need to be true to run.尝试将 turnCount 的条件放在 if (operSeq != inSeq) 或 && 它们一起,如果它们都需要为真才能运行。

Would have done this as a comment, but don't have the rep yet :/本来可以作为评论来做的,但还没有代表:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM