Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C Question

Options
  • 19-12-2005 8:07pm
    #1
    Closed Accounts Posts: 1,541 ✭✭✭


    Hi All,

    Does anyone know what the following line does?
    silent [0] = (total_audible_pow_ref < 1700);
    

    What does the < 1700 mean?

    Thanks for any help.


Comments

  • Registered Users Posts: 304 ✭✭PhantomBeaker


    Well, it'll resolve to 0 or 1.

    Without context I can't tell you what the variable means... But basically from what I see, you have an array called silent. You're setting the first element to 1 if that variable is less than 1700.

    I'd really suggest you look up some basic C documentation - I'd point you in the direction, but my bookmarks are messed up right now (I rely on del.icio.us quite heavily, and they're down)

    Aoife


  • Closed Accounts Posts: 3,322 ✭✭✭Repli


    It looks like a weird conditional if statement


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    "silent" is and array variable
    silent[0] means the first element in the array

    total_audible_pow_ref is a variable probably an integer or real number

    total_audible_pow_ref < 1700 is checking if the variable is less than 1700

    If it is less than 1700 then it will return a value of "1". This value will be placed in the variable silent[0]
    If it is not less than 1700 it will return 0 and place it in the variable.

    So the variable must be either 1 or 0


  • Registered Users Posts: 304 ✭✭PhantomBeaker


    Repli wrote:
    It looks like a weird conditional if statement

    Thing is, stuff like that can be really handy to know.

    Basically the way C handles conditionals is that a conditional expression boils down to a number.

    When C processes an if statement, it checks to see if it resolves to 0. If it does, the statement is false, otherwise it's true.

    So 0 is false, 1 and anything else is true. I just did a check... I compiled a quick program to see how it branched when I gave it 2 and -2... both resolved to true, but I don't know if that's considered default/ISO-standard behaviour, so don't rely on something that isn't 1 being true in all cases.

    So when a compiler sees "x == y" it will resolve to 0 or 1. If X is equal to Y, you get 1, or if it's not, you get 0. Same with any of the other comparison operators like <, <=, >, >=, && and ||... they'll all be resolved down to one answer.

    As a result you can do interesting things:

    Let's say you want to do something if a variable is odd, you can just do
    if(var % 2){
    insert code here
    }

    It means exactly the same thing as "if((var % 2) == 0)". Because it'll resolve to 0 or 1.

    I think I'm labouring the point now, but it's something to be aware of.

    In java, it uses boolean datatypes instead for conditionals... so Java programmers might not be aware of this stuff... and it's useful to know.

    Take care,
    Aoife


  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    It means exactly the same thing as "if((var % 2) == 0)".
    I think that should be "if((var % 2) == 1)"?


  • Advertisement
  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    Thanks everyone for all your help.

    I also have come across :
    hz_spectrum [k] = fft_tmp [k << 1] * fft_tmp [k << 1] + fft_tmp [1 + (k << 1)] * fft_tmp [1 + (k << 1)];
    

    K is a variable changing/iterating inside a for loop. What does the <<1 mean though?:confused:

    Thanks again.


  • Registered Users Posts: 2,758 ✭✭✭Peace


    I'm a C# developer so not really that good with plain C code. But that stuff is not very clear and should be commented IMO or rewritten so its more readable.


  • Closed Accounts Posts: 80 ✭✭Torak


    finnpark wrote:
    Thanks everyone for all your help.

    I also have come across :
    hz_spectrum [k] = fft_tmp [k << 1] * fft_tmp [k << 1] + fft_tmp [1 + (k << 1)] * fft_tmp [1 + (k << 1)];
    

    K is a variable changing/iterating inside a for loop. What does the <<1 mean though?:confused:

    Thanks again.

    k << 1 means shift the bits which represent k by 1 place to the left and fill the "space" with a zero bit so if k is 3 then 3 (00000011) becomes (00000110).

    in your loop if k = 3 then
    hz_spectrum [3] = (fft_tmp[6] * fft_tmp[6]) + (fft_tmp[7] * fft_tmp[7])

    k = 2
    hz_spectrum [2] = (fft_tmp[4] * fft_tmp[4]) + (fft_tmp[5] * fft_tmp[5])

    k = 1
    hz_spectrum [1] = (fft_tmp[2] * fft_tmp[2]) + (fft_tmp[3] * fft_tmp[3])

    k = 0
    hz_spectrum [0] = (fft_tmp[0] * fft_tmp[0]) + (fft_tmp[1] * fft_tmp[1])


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    Thanks for that.

    That makes sense alright;) . Effectively the same thing as multiplying by 2:cool: , must be a more efficient way of doing it.

    Thanks again for this, everyone has been most helpful. None of the C is commented so thanks for your help.:)

    finnpark


  • Registered Users Posts: 2,002 ✭✭✭bringitdown


    Peace wrote:
    I'm a C# developer so not really that good with plain C code. But that stuff is not very clear and should be commented IMO or rewritten so its more readable.
    LOL!


  • Advertisement
  • Registered Users Posts: 304 ✭✭PhantomBeaker


    Peace wrote:
    I'm a C# developer so not really that good with plain C code. But that stuff is not very clear and should be commented IMO or rewritten so its more readable.

    Heh, I'm guessing there won't an IOC#CC then (just for reference the IOCCC (http://www.ioccc.org/ ) is the International Obfuscated C Code Contest. Some of the sickest twisted code comes from there...) :D

    To be honest, when I'm reading that code that the OP posted, I find it quite easy to read... and I don't have much of a C background either - I've only had to use it seriously for the past couple of months and even then it wasn't too down-n-dirty. I've mostly played with Java and some perl, and I've come accross most of those constructs in those languages as well.

    Ok, the variable names are terse, but overall it looks ok. It looks like they might have been optimising for speed slightly - mainly because from the variable names it looks like some sort of sound processing program, which would need to be efficient if it was manipulating sounds in real time.

    Commenting would be a help, but when have programmers ever properly documented their code? :D At any rate who's to say it's not there, we've only gotten two individual lines of code from the OP.


  • Registered Users Posts: 2,758 ✭✭✭Peace


    LOL!

    You got a problem?


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    It really isn't that unreadable, and it is an obvious way of doing what needs to be done. Have a look at an OS kernel, if you want odd.


  • Closed Accounts Posts: 281 ✭✭incisor71


    That's quite a strange array indexing scheme. Mind you, that's probably about as cryptic as C becomes for application-level programming (which I've been doing for close to 15 years), unless it's written really badly.

    It's off-topic of me to ask, but would I be correct in saying that it has something to do with a Z-transform algorithm, or at least related to switching between time/frequency domains?


Advertisement