From the original SynthMaker forum (2004–2014), with its members’ names and dates.
cbit ·
Jorge kindly just gave me a mini tutorial about creating conditional type statements in SMs code component, using the '&' character.
While the code in the simple example ive been fiddling with (a ramp generator) sort of makes sense to me.. im still not understanding how 'And'ing works really.
streamin in;
streamout out;
float f;
f=f+in;//0.4 is attached to 'in', so f increases by .4 with each iteration..
f=(f<1.0)&f;// this is the part i don't completely get
out=f;
f=(f<1.0)&f : this seems to be analogous to 'if f<1, set f to f, otherwise set f to zero', which resets the ramp waveform when it reaches 1.
I understand that a comparison will evaluate to 0 if false and 1 if true, and i think this is important in this context too somehow (?)..
I suppose i'm confused about exactly how '&' works here (i come from an actionscript and php coding background) .. any pointers or keywords to google for, or articles to read would be much appreciated. Thanks![/code]
stefan ·
it results in a "bitwise and"
That means that you'll get either:
f = 0 & f
or
f = all_bits_set_to_one & f
if you google on bitwise and, you will get more information on how that works. But basicly
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
beno ·
To elaborate a little further, your code has:
f=(f<1.0)&f
so let's say f = 0.101011 when converted to binary for example
so since this is less then 1, you will get
f = 1&f
This does a bitwise AND of 1 and every bit of f. According to stefan's desciptions above, 1 ANDed with any value just gives back that value, so this just gives back f again so f doesn't change.
Now if f = 1.101011 when convereted to binary, it is now greater than 1. So the first equation now becomes
f = 0&f
Again following the descriptions above, we see that 0 ANDed with anything is zero. So going bit by bit we are simply left with f = 0.
Hope that helps.
Ben
cbit ·
thanks for your replies,
ben: yours in particular was very helpful.. combined with the super simple binary primer i found here: http://www.litfuel.net/tutorials/bitwise.htm, (by googling 'bitwise and php', thanks stefan 🙂 )it now makes sense to me.
thanks a lot!
Wrote something here? To have a post removed or your name hidden, write to [email protected].