title: Looking for a *general* algorithm for generating standard PRBS sequences
body: <p>In electronics there are standard pseudo-binary sequences. In the literature they talk about the generating polynomials for these sequences:</p>
  
  <pre><code>PRBS-7  : x^7 + x^6 + 1
  PRBS-10 : x^11 + x^10 + 1
  PRBS-15 : x^15 + x^14 + 1
  PRBS-23 : x^23 + x^18 + 1
  PRBS-31 : x^31 + x^28 + 1
  </code></pre>
  
  <p>I was able to find on <a href="https://en.wikipedia.org/wiki/Pseudorandom_binary_sequence" rel="nofollow">Wikipedia - Pseudo-random binary sequence</a> a snippet of C code for generating PRBS-7 but there was no indication of how to convert this to create the other sequences.</p>
  
  <p>Here is what I have, but other than for PRBS-7 it generates nothing but zeroes. This code is C# but I would be ok with C or C++ samples.</p>
  
  <pre><code>private static void Generate(int v1, int v2, StringBuilder roots, StringBuilder bits)
  {
      // This works fine for PRBS7 with v1=7, v2=6 but fails for 15,10; 23,18; 31,28
      const byte start = 2;
      byte a = start;
      for (int i = 1; i &lt; 2000; i++)  // I added the 2000 limit, otherwise was infinite loop
      {
          roots.AppendFormat("{0:X2} ", a);
          int newbit = (((a &gt;&gt; v1 - 1) ^ (a &gt;&gt; v2 - 1)) &amp; 1); // my naive attempt to generalize the algorithm
          a = (byte)((byte)((a &lt;&lt; 1) | newbit) &amp; 0x7f);
          bits.Append(newbit);
          if (i % 32 == 0)
          {
              roots.AppendLine();
              bits.AppendLine();
          }
          if (a == start)
          {
              roots.AppendFormat("\r\nRepetition Period = {0}", i);
              break;
          }
      }
  }
  </code></pre>