## Binary in Max?

Oct 13, 2013 at 4:37pm

# Binary in Max?

I want to send binary numbers in form of messages as opposed to a listed message.

I find it kind of hard because if zeroes are first its treated as a number with unnecessary 0 digits.

#267950
Oct 13, 2013 at 8:48pm

maybe using combine?
(not sure if this is really what you want)
<code>

– Pasted Max Patch, click to expand. –

</code>

#267957
Oct 14, 2013 at 12:25am

If you need leading zeros, just put your number into double quotes in a message box. Also, if you want to convert 101 (int) into “00000101″ (symbol) you can use [sprintf @symout %08d]. Just keep in mind that it is not a good idea generating large amounts of symbols, you’ll waste memory and slow down Max. If you need more info, there are plenty of forum discussions about this.

Also (but I’m probably stating the obvious here) losing leading zeros in binary representation is harmless, just as it is in decimal: 101 and 00000101 are exactly the same.

hth
aa

#267965
Oct 14, 2013 at 12:26am

EDIT: it’s not [sprintf @symout %08d]. It’s [sprintf symout %08d]!

#267966
Oct 14, 2013 at 1:34am

i meant something like this:

<code>

– Pasted Max Patch, click to expand. –

</code>

I need the code to operate a set of switches, so “0101″ means switch 1 is off. “101″ means switch 1 is 1 and there is no data for switch 4.

I’m avoiding sprintf/combine/tosymbol just to avoid having symbols and clogging memory

#267970
Oct 14, 2013 at 2:39am

I’d set up addresses for each switch, and then send out the messages like “switch1 1, switch2 0, switch4 1″, avoiding that particular type of formatting.

#267979
Oct 14, 2013 at 2:45am

but the question remains; why NOT a list? Because sending out as a list would solve all the problems, as far as I can tell?

#267981
Oct 14, 2013 at 2:56am

Elegance.

I either need to unpack and repack to send this to a sub-patcher (Which gets increasingly annoying with increasing number of switches), I also wish to add flexibility so a “fixed” number of switches kind of limits it (or I need to send all values even if they’re not used at all times – redundancy). Using this I can have one inlet, dynamic creation of objects based on wordlength and neatly transfer everything on one unpack-pack outlet.

It’s just something I wish I had :-)

#267982
Oct 14, 2013 at 3:11am

so you need a nibble-sized packet? that’s not possible, as the smallest data type in max is an int… but what are these switches? are they inside max or somewhere else? if it’s in max that sort of representation has to be a list as far as i can understand it.

sorry just read the previous 3 posts… max is _inelegant_ at the best of times, a nasty piece of work at the worst

#267983
Oct 14, 2013 at 3:18am

Yes, this makes sense.
Still, I’d say that you’re abusing binary ;)
In fact, you’re saying that each switch has three states (on, off, undefined), so why not use a ternary representation rather than binary? This would allow you to express the “undefined” state also for the significant figures of your number (in your example above, what if it was switch 1 being undefined?)

#267984
Oct 14, 2013 at 3:40am

Hm, just figured I could actually always use a leading 1.

<code>

– Pasted Max Patch, click to expand. –

</code>

edit: undefined is in a sense of “this switch is currently not used because there are only X outputs available, so “MSB” gets discarded either way.

I’m doing some multi-out mumbo-jumbo and each poly~ instance feeds a certain combinations of outputs, come to 16, you have unnecessarily long list to process every time, and if you want to dynamically reduce the outputs your “unpack 0 0 0 0″ order gets corrupted.

Now I can just convert this 1xxxxxx binary to DEC and have a nice single value for it. :P

Maybe I should start coding; i don’t like workarounds… But I’m a lazy man…

I have come over this a few other times and it just annoyed me that a set of switches has to be in a list message because it makes so much sense for it to be a single message (to me), so this is just one of the instances I want to use it as such.

I can’t believe it never occurred to me to just use a leading 1…

#267986
Oct 14, 2013 at 3:53am

… but is it “elegant” code to specifically format a message to tell a recipient “do nothing”?

#267987
Oct 14, 2013 at 3:57am

I don’t know if your interested in external objects or not, Peter Elsea’s Lobjects contain an object [bit] that breaks in int into separate bits or [Lbits] that list the bits in an int including the leading zeros. ftp://arts.ucsc.edu/pub/ems/Lobjects/

#267988
Oct 14, 2013 at 4:05am

(“this makes sense” referred to the need to have all the data for each switch packed into a single atom, not to the inelegance of Max… I disagree with you, at least as long as you treat Max for what it is: a great tool for combining processes and user interface widgets, where complex processes are often best expressed in traditional programming languages but also possible with plain Max…)

#267993
Oct 14, 2013 at 5:51am

“Hm, just figured I could actually always use a leading 1.”

Or add 1 to avoid zeros ;-)

<code>

– Pasted Max Patch, click to expand. –

</code>

#268009
Oct 22, 2013 at 4:42pm

@Wetterberg: … but is it “elegant” code to specifically format a message to tell a recipient “do nothing”?

That’s exactly what I’m try to avoiding. By sensing a message this way I can have a fixed size Pack/Unpack for larger lists while this particular part of the list can vary in size, avoiding the need to have empty “maximum message length” slots present all times.

@Nicolas Danet: Hah. Yeah. That as well. Elegant solution. I prefer the leading 1, looks more like I know what I’m doing.

@Rick: great link, but I rather keep it as vanilla as/if possible.

@Andrea Agostini: not sure that I implied anywhere that I thought you agreed on the inelegance of Max?

I really like Max, it just pisses me off sometimes. Less then most of other environment I work in anyway. :-)

#268834

You must be logged in to reply to this topic.