Nick Nick - 1 year ago 61
C Question

How do I read this software phase lock loop code?

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.


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:

// 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
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?


Answer Source

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);

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!


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|
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download