Help Understanding Conditional Statement


#1

Could someone help interpret a snippet from some Arduino code I've been working on porting to Axo?

unvoicedSource = (synthRand & 1) ? synthEnergy : -synthEnergy;

I think what's going on is that the conditional evaluates if the least-significant bit of synthRand is a 1 or a 0, and sets unvoicedSource accordingly. I didn't know you could use integers directly as booleans like this though- I'd normally expect to see a numeric comparison, rather than a bitwise operation inside these brackets.

a|x


#2

That seems the case, maybe it is just some code to convert from unsigned int to signed?

The integer as boolean is already done in many axoloti objects, where a 000000.. word equals to boolean 0 and anything else equals 1

Also, you can use conditions as numbers: (a<b) outputs a bit (1 or 0), which can eventually be multiplied by other stuff.

If, for example you want to model the friction force (which is 0 if speed=0 and const if speed=/=0 ) you could write something like this: friction = (!v=0)*const


#3

Cool, thanks for the info, @Sputnki, good to know.

a|x


#4

It's effectively a 1-bit noise function, I think, with the conditional determining if the output is positive or negative, with the magnitude of the sample being determined by the synthEnergy variable.

a|x


#5

Handy. Is that more expensive that an ordinary conditional/ternary statement i.e.

friction = (v == 0) ? 0 : const;

since there's a multiplication involved?

a|x


#6

I have no idea about that, you should try doing a comparison