Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

## Bitwise usage for IF

DSP related issues, mathematics, processing and techniques

### Bitwise usage for IF

Hello!

Can you give me an example how to organize the expression like this in DSP code?

If asum1 <=1 then x = 1.0
else x = asum1

Pardon my free "code" In practice I'm trying to make a module that will be keeping a sum of three inputs below 1.0
Dancing around "&" gives me the asum1 or zero on the variable. I can't figure out how to make it properly. I'll add the code later when i'll be at home.
Kirill_Neoris

Posts: 25
Joined: Fri Jan 08, 2016 5:06 pm

### Re: Bitwise usage for IF

I can think of three possibilities:
Code: Select all
`x = max(asum1,1.0);`

Code: Select all
`x = asum1 + (1.0 - asum1)&(asum1 <= 1);`

Code: Select all
`x = 1.0 + (asum1 - 1.0)&(asum1 > 1);`

And a 4th one which may be easier to grasp but has more operations:
Code: Select all
`x = 1.0&(asum1 <= 1) + asum1&(asum1 > 1);` martinvicanek

Posts: 1081
Joined: Sat Jun 22, 2013 8:28 pm

### Re: Bitwise usage for IF

Thank you! I'll check this out when will be at home!
Extra thanks for the quick answer!
Kirill_Neoris

Posts: 25
Joined: Fri Jan 08, 2016 5:06 pm

### Re: Bitwise usage for IF

martinvicanek wrote:I can think of three possibilities:
Code: Select all
`x = max(asum1,1.0);`

Code: Select all
`x = asum1 + (1.0 - asum1)&(asum1 <= 1);`

Code: Select all
`x = 1.0 + (asum1 - 1.0)&(asum1 > 1);`

And a 4th one which may be easier to grasp but has more operations:
Code: Select all
`x = 1.0&(asum1 <= 1) + asum1&(asum1 > 1);`

Code: Select all
`streamin lfo1;streamin lfo2;streamin lfo3;streamout lfo1n;streamout lfo2n;streamout lfo3n;streamout k;streamout sum;float lfosum;float k1;float x;lfosum = lfo1+lfo2+lfo3;x = 1.0&(lfosum <= 1) + lfosum&(lfosum > 1);k1 = 1/x;k = k1;sum = lfosum;lfo1n = lfo1 * k1; //this is to make the sum of amplitudeslfo2n = lfo2 * k1; //equal or lower than 1.0lfo3n = lfo3 * k1;`

I've tried the last right away and it works perfectly! My version was:
Code: Select all
`x = (lfosum>=1) & 1.0;`

I still can't figure out how your code works... Maybe it need a time to settle down in my head...
Does the "plus" means it's and additional condition?
Kirill_Neoris

Posts: 25
Joined: Fri Jan 08, 2016 5:06 pm

### Re: Bitwise usage for IF

Version 3.0.9 has the ternary operator build in, so this should work:
Code: Select all
`x = (asum1 <=1) ? 1.0 : asum1;` MyCo

Posts: 758
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany