Easy counting made complicated…

Nov 14, 2010 at 11:16pm

Easy counting made complicated…

Hey folks! I'm trying to do some basic math with an external device with input and could use a little help. This is basically what I have so far:

Each of these 5 channels have an unique specific value as well as a standard “0″. Basically my aim is to add all five of these channels into a singular number box for further processing. i.e. if just channel 1 is triggered, the total value is 1, and if channel 2, 3 and 4 is triggered simultaneously the total value is 14, etc etc.

So essentially the goal is to have these five channels have 32 different different values depending on what combination of channels are active. I've attempted to string together some + objects, whilst it did work in retrospect it faulted majorly when the values descended.

Any thoughts or suggestions?


  1. max_help.jpg
Nov 15, 2010 at 12:47am

i don’t quite understand what you’re trying to do. i especially need to see what goes on above the select objects to get a clearer picture but it sounds like you’re just needing a ‘binary to decimal’ conversion patch.
like this:

#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P comment 224 263 42 196617 < -Total;
#P newex 93 87 85 196617 pak 0 0 0 0 0;
#P toggle 165 67 15 0;
#P toggle 147 67 15 0;
#P toggle 129 67 15 0;
#P toggle 111 67 15 0;
#P toggle 93 67 15 0;
#P flonum 174 263 49 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 123 136 39 196617 set $1;
#P newex 174 242 40 196617 accum;
#P newex 108 217 27 196617 < <;
#P newex 141 199 27 196617 – 1;
#P newex 108 181 29 196617 t 1 i;
#P newex 108 159 45 196617 zl sub 1;
#P newex 93 111 40 196617 t b l 0;
#P comment 189 67 105 196617 < -Set Channels on/off;
#P connect 2 0 3 0;
#P connect 3 1 4 0;
#P connect 3 0 5 0;
#P connect 14 0 1 0;
#P connect 13 0 14 4;
#P connect 12 0 14 3;
#P connect 11 0 14 2;
#P connect 10 0 14 1;
#P connect 9 0 14 0;
#P fasten 1 0 6 0 98 241 179 241;
#P connect 6 0 8 0;
#P fasten 5 0 6 1 113 238 194 238;
#P connect 4 0 5 1;
#P fasten 7 0 6 0 128 154 179 154;
#P connect 1 1 2 0;
#P connect 1 2 7 0;
#P window clipboard copycount 16;

binary math is useful for this kind of ‘combination-ID’ task, you have a very good idea in your hands… my patch above is still not the only way to do this(Luke Hall could tell you more about binary/decimal list-processing magic) but such a great idea deserves an efficient implementation so…
hope it helps.

Nov 15, 2010 at 1:06am

I’ve written a javascript that converts numbers to and from different bases, in your case decimal to binary and back again. You can find it as part of a collection of possibly useful bits here although it is written for max 5 the javascripts should work in max 4 just fine.

Nov 15, 2010 at 1:24am

Here are two painful looking [jstrigger] snippets which should do what you need without having to worry about loading external javascript files. The first takes a decimal number and outputs the binary representation as a list of ones and zeros with no need for [fromsymbol]. The second does the reverse. Paste them into an object box and you should be ready to go!

jstrigger (a[0].toString(2).split(”).map(function(x){return parseInt(x)}))

jstrigger (parseInt(parseInt(a.join().replace(‘,’,”,’g'),2).toString(10)))

Nov 15, 2010 at 4:42pm

Hey Raja! Just implemented that chain in and it works perfectly! Thank you so much mate, you are a legend! Hell give me your e-mail sometime and I’ll send you the completed project when I’m done!

Nov 16, 2010 at 12:05am

Rohman, no prob.

and thanks, Luke, for showing those… this inspires me to look more at the jstrigger object. i’ve never used it before!

one test i ran, though, based on chris muir’s benchmark timer patch shows that the jstrigger method seems to take almost 10x as long on my comp(by the way, the ‘totals’ are different because the bit order for both are opposite, one is l2r the other r2l, but i was thinking this shouldn’t make much difference in overall timing of the basic calculation too much so i left it as is):

– Pasted Max Patch, click to expand. –

still, i think it’s good to keep the jstrigger method in mind because i read recently c74 are working on more efficient javascript implementation… maybe?…
JKC wrote in this thread:
“FWIW, we are looking into having a much faster JS engine for the next major version of Max.”

(Rohman, would love to see it when it’s done, i’m a bit skittish about putting my email online, maybe just post the project here or a link to it if you feel inclined, i’m sure others would love to learn from it, too… but no worries, either way)

anyways, glad it helped. best of luck.

Nov 16, 2010 at 9:44am

here´s the 110 version for decimal to binary. should be no problem to expand it to up to 24 digits.

@luke: i dont get your download link working, might be a temp problem of the hoster.

Nov 16, 2010 at 10:30am

I added a couple more techniques, two are faster and one is in the middle:

– Pasted Max Patch, click to expand. –

Wonder where the culprit objects are? Is there any way to tell how quickly a given input to an object comes out the outlet? I mean, probably it’s way too fast to measure with scheduler objects, but maybe there could be a “clocker” function that would print out the processing time, in microseconds when needed, to the Max window… would sure like to run some tests on various objects. anyway…

Nov 16, 2010 at 5:36pm

Simplest and fastest are the Byte object in Lobjects, and jasch’s bitlist object.



Each object has its own way of designating position of the most significant/least significant bit – hence the differing results. Same is true in your example @seejayjames.

– Pasted Max Patch, click to expand. –

You must be logged in to reply to this topic.