1

In my search for an example of a software phase lock loop I came across the following question

Software Phase Locked Loop example code needed

In the answer by Adam Davis a site is given that is broken and I have tried the new link that is given in a comment but I cant get that to work either.

The answer from Kragen Javier Sitaker gave the following code as a simple example of a software phase locked loop.

main(a,b){for(;;)a+=((b+=16+a/1024)&256?1:-1)*getchar()-a/512,putchar(b);}

Also included in his answer was a link to what should be a much more readable example but this link is also broken. Thus I have been trying to translate the above code into simpler and more readable code.

I have come this far:

main(a,b){
    for(;;){
    // here I need to break up the code somehow into a if() statement('s).
       if(here I get lost){
          a = a+1;
          if(here i get lost some more){
           b = b+1;
          }
       }
}

Thanks to the SO question What does y -= m < 3 mean?

I know it is possible to break up the a+= and b+= into if statements. But the (&256? 1 : -1)*getchar()-a/512,putchar(b); part in the code is killing me. I have been looking on Google and on SO to the meaning of the symbols and functions that are used.

I know the & sign indicates an address in memory.

I know that the : sign declares a bit field OR can be used in combination with the ? sign which is a conditional operator. The combination of the two I can use like sirgeorge answer in what does the colon do in c?

Theory Behind getchar() and putchar() Functions

I know that getchar() reads one character

I know that putchar() displays the character

But the combination of all these in the example code is not readable for me and . I can not make it readable for my self even do I know what they all separately do.

So my question: How do I read this software phase lock loop code?

main(a,b){for(;;)a+=((b+=16+a/1024)&256?1:-1)*getchar()-a/512,putchar(b);}
Community
  • 1
  • 1
Nick
  • 13
  • 1
  • 4
  • "*I know it is possible to break up the a+= and b+= into if statements.*" where did get this idea from? You might like to read about "Compound Assigment Operators" (as that's what they are) here: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Compound_assignment_operators – alk May 22 '14 at 10:04
  • `a+=` is synonym of `a = a +`, not if. – DrakaSAN May 22 '14 at 10:08
  • From the http://stackoverflow.com/questions/7863285/what-does-y-m-3-mean question. In the 3th comment on the question given by **Mark** he rewrites y- = m<3;. It is possible that I misinterpreted this. – Nick May 22 '14 at 10:11
  • I'd say it's a bad idea to try to learn basic C from SO comments ... – alk May 22 '14 at 10:13
  • I will keep that in mind for the future. And thank you for the wiki link it is very clear. If I understand correctly I could change this example code : a +=(b*c) into this: a = a +(b*c); – Nick May 22 '14 at 10:23
  • 1
    It is a bad idea to start learning C by reading the IOCCC snippets. (hint `&` is used twice in the C syntax. One is "address of", the other is "bitwise and") – joop May 22 '14 at 10:24
  • Why do you think that someone that posts 1 line of goo like that have anything of value to share? In this case, it's pretty obvious that he did it as a sarcastic response to a code beggar, answering the question without giving them any useful code (the question is asking where to find code libraries so it is even off-topic for SO). – Lundin May 22 '14 at 11:48

1 Answers1

1

What I get is:

main (a, b)
{
    char c;
    for (;;)
    {
        c = getchar();
        b = (b + 16 + (a / 1024));
        if(!(b & 256)) 
        {
            c = c * -1;
        }
        a = a + c - (a/512);
        putchar(b);
    }
}

I had to add a c variable to not get lost.

What the program does:

Take a and b.
Infinite loop:
    get a char input in c
    calculate b
    If (b bitwise AND 256)
        c = -c
    Calculate a
    Print b

It seems it translate input into something else, I have to see the code in action to understand better myself.

Hope it helped!

Hint:

https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

a+= => a = a +

a?b:c => if(a){return b;} else {return c;} (As a function itself, it don t truly return)

Add parentheses, it help.

a & b is bitwise AND:

a/b |0|1|
   0|0|0|
   1|0|1|
DrakaSAN
  • 7,673
  • 7
  • 52
  • 94