Boolean input to number output

steve holmes's picture

Hi everyone I have composition using a multiplexer sending strings to a serial send patch, I would like to use this in VDMX using buttons to send each state but to do that the input needs to be a boolean. I have tried lots of ways to take multiple boolean inputs and convert them to numbers that can be feed into the multiplexer but have failed. So if anyone has an idea what code I need to convert multiple booleans to numbers I would really appreciate the help. Cheers Steve

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.'s picture
Re: Boolean input to number output

The maths expression patch supports bitwise operations. If you post a comp I can try and tailor something if it's a simple matter of this and that boolean merged bitwise to make an 8-bit number. (Using OR operators). To do manually, the maths is just:

a + 2b + 4c + 8d + 16e + 32f + 64g + 128*h = N,

Where N is your numeric that can be decoded to 8 boolean values; a,b,c...h have values of either one or zero ie {0,1}={fasle,true}

So if a,b,c,d are false and e,f,g,h are true, N=16+32+64+128 =240 ( =255-1-2-4-8)

Decoding is more procedural, so that's where bitwise operations come into their own as you can compare the on/off bits in N (8 bits to a byte and integers are at least one byte long) against target bits with the AND operators.

Probably easier if I you see it in a comp. I'll try dig one up I've already made.

steve holmes's picture
Re: Boolean input to number output

To be really honest I am not sure if your answer helps me or not, as I don't really understand it. What I need to do is take 15 boolean inputs that need to behave like radio buttons (only one active at a time) and from this I need to feed a multiplexer numbers so it can change state. Cheers Steve's picture
Re: Boolean input to number output

If I'm not completely misunderstanding you now, you just have an index value scrolling through various pre-set values in the multiplexer? Something to do with VDMX means you can't send an index value you need to send boolean values for each possible index state, right? (If you could just send an index value it would obviously be much easier).

If you only ever have one radio button on at a time, you can just multiply each each index value by the boolean (since false=0 and true=1). Then add all the multiplied values and you have your index value.

optical illustration 1.qtz13.46 KB

This QC comp demonstrates a Javascript way and a maths expression way to convert 8 booleans to one integer. Note: these boolean checkboxes are not radio buttons so you have to reset them manually or they simply add there values to create a higher index value.

Try and describe to yourself what is changing with the colour fields in this comp before you look in the Editor. (Surprising huh)

(I thought from your original post you wanted to pass the boolean values (we know there's 15 now) inside one single integer (whole number) via the serial patch. The previous maths was to get an integer that 'contained' all the boolean values. This integer could then be 'unpacked' at the other end of (something) to reveal the previously packed boolean states.)

Off topic It tried making a patch before but got stuck in the Javascript Patch which frankly bugs the living @#$% out of me. I often spend hours trying to find the right syntax to handle structured Objects/Arrays properly when the actual logic of the script itself is incredibly simple. All the JS reference docs in the world can't help when you don't know what it is that is causing the problem. A decent debugger wouldn't be bad maybe I'll try the Adobe one not sure how compatible it is. Any advice from anybody on learning this stuff would be very gratefully received. I can program Pascal, Basic, Fortran old school stuff so programatic issues don't bother me it's JS unhelpful syntax and var type nuances that I don't get, especially in QC.

steve holmes's picture
Re: Boolean input to number output

That works great thanks for the help. I have to say one of the 'problems' (and benefits) of how easy quartz composer is to use is that it lets people like me with no programming experience start to get interested in making little applications without a real understanding of simple building blocks of applications including understanding things like maths etc.

Anyway thanks so much for the help. Steve's picture
Re: Boolean input to number output

No worries. I think we need to have more second tier participants on Kineme forums. I can help with easy programming things that the hard core programmers don't bother weighing in on because it's first year CompSci level questions. I wish there more people who could help me with the JS peculiarities. Perhaps I need to find a JS forum elsewhere, only other one I know is the Adobe scripting one.

Also if you extended change the JS in that comp using the "2 to the power of i" stuff I was talking about earlier you could have it knock out all the lower checkboxes choices and just take the highest pick when there's more than one as 'true'. Alternatively you could assign keyboard keys to them and have the javascript just hold the most recent value. (Or use the new Kineme Freeboard patch). Let me know if you want to look into that and I'll help. Just needs an if statement inside the loop.

I agree about how democratic QC is, it's about the only thing that has lured me back to programming in 20 years (well almost). Now I wish I knew a whole CompSci degrees worth of stuff to really get going with it... just sucks so much time and I'm not on a wage to do it!

Notice the optical illusion btw?