-3

ok.. I know it sounds annoying to ask questions like this, BUT I really don't understand the code [advance_reg(int*)] below. In the comments of the code, it says that "/the register steps according to the primitive polynomial(64,4,3,1,0);/", I just didn't get it. Any one please give me a hint about this code please? For example, why it defines the adv_64 arrays like that? I understand the first for block shifted the number 27 times, one bit each time. But why? what is the math behind this? Further, why perform the second for block? What does these all connect with the polynomial(64,4,3,1,0)?

 static int bitcnt( int x) 
 {
    unsigned i=0,y;

    for (y=(unsigned)x; y; y &= (y-1) ) 
      i++;
    return(i);
 } 

 static void advance_reg(int *reg_fill)
 {
  const int mask = 0x1b;
  int adv_64[4][2];
  int i,new_fill[2];
  unsigned temp;

  adv_64[0][0] = 0xb0000000;
  adv_64[0][1] = 0x1b;
  adv_64[1][0] = 0x60000000;
  adv_64[1][1] = 0x2d;
  adv_64[2][0] = 0xc0000000;
  adv_64[2][1] = 0x5a;
  adv_64[3][0] = 0x80000000;
  adv_64[3][1] = 0xaf;
  new_fill[1] = new_fill[0] = 0;
  temp = mask<<27;

  for (i=27;i>=0;i--) 
  {
     new_fill[0] = (new_fill[0]<<1) | (1&bitcnt(reg_fill[0]&temp));
     new_fill[1] = (new_fill[1]<<1) | (1&bitcnt(reg_fill[1]&temp));
     temp >>= 1;
  }

  for (i=28;i<32;i++) 
  {
     temp = bitcnt(reg_fill[0]&(mask<<i));
     temp ^= bitcnt(reg_fill[1]&(mask>>(32-i)));
     new_fill[0] |= (1&temp)<<i;
     temp = bitcnt(reg_fill[0]&adv_64[i-28][0]);
     temp ^= bitcnt(reg_fill[1]&adv_64[i-28][1]);
     new_fill[1] |= (1&temp)<<i;
   }

  reg_fill[0] = new_fill[0];
  reg_fill[1] = new_fill[1];
  }
user1595754
  • 81
  • 1
  • 1
  • 7
  • why ppl give me negative points to this question? I am really sincere asking it. Shame on those arrogant self-absorbed ppl! If I had better solutions or ppl to ask for I won't post it on stackoverflow then! – user1595754 Feb 04 '13 at 03:25
  • 1
    You haven't explained anything about the code, what it's supposed to do, why you have a problem with it, or why you need help. Can you fill in any of that information? – Carl Norum Feb 04 '13 at 03:29
  • This is a seeding code for a pseudorandom number generator [part of the SPRNG2.0, I didn't get it.]. and I think I had described why I had a problem above. – user1595754 Feb 04 '13 at 03:38
  • shame on stackoverflow. Guess ppl today just laughing at others' question to cover up that they actually don't have any answer about it! I ll figure it out myself! Anyone tried to answer it, you'd better prepare yourself with qualified knowledge but just the ability of voting down ppl's question! – user1595754 Feb 04 '13 at 03:48
  • I know it's upsetting to get a downvote. But they're not criticizing *you*; they're criticizing *the question*. The comment "This is a seeding code for a pseudorandom number generator [part of the SPRNG2.0" really needs to be in the question itself (click the "edit" box!). Imagine how the question will appear to a complete stranger who knows C but doesn't know anything about your project. – luser droog Feb 04 '13 at 05:08
  • Most important is the title. Try something more like "How does the linear feedback shift register relate to this polynomial?" That way people have some idea of what it is before they even click on it. – luser droog Feb 04 '13 at 05:12

1 Answers1

2

It's a linear feedback shift register. (Or given the description, that's what it's supposed to be.)

Such a function is defined in terms of a binary polynomial and generates a fixed, deterministic permutation of the positive integers up to a given power of 2. As such, it's among the least random things that can be called "random."

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Yes! Thanks sincerely!! It is a LFSR. But why the adv_64 is defined like that? How does it relate to the polynomial(64,4,3,1,0)? – user1595754 Feb 04 '13 at 03:55