Help with simple bit of (binary?) mathematics

tedthetrumpet's icon

A drummer plays a basic groove for one bar, then plays a variation in bar two. In bar three she goes back to the basic groove, then plays a different variation in bar four. Then she reapeats all of the above for the fifth to seventh bars, playing a new variation in bar eight... and so on.

If we tabulate this so far, labelling the bars from one to eight and the variations as 0, 1 and 2, then we get this;

bar    var    bin
1    0    0001
2    1    0010
3    0    0011
4    2    0100
5    0    0101
6    1    0110
7    0    0111
8    3    1000

What is the formula (or patch) which would allow one to calculate which variation to play based on the bar number? The only insight I have so far is that if you write out the bar numbers in binary as in the third column, then the number of the variation is given by where the rightmost '1' is; in other words, in bar 7 the rightmost '1' is in the 0th column = var 0, in bar 8, the rightmost '1' is in the 3rd column = var 3.

I've been messing around with all sorts of stuff, and I just can't get it; I know the answer to this must be some *really simple* bit of maths which I just can't see...

Thanks

seejayjames's icon

Probably something with [zl] could do it, based upon the position of the 1. But you could also look to [&] which can turn decimals into binary, by running the number through

& 128
& 64
& 32

etc... collect them all, it will give you the binary version of all numbers up to 255. Not sure if this will help for what you want, but it might... :)

Luke Hall's icon

You can either use [bitlist] from the Jasch library or any other way of generating a binary representation from an integer and then use [zl sub]. Here's an example.

Max Patch
Copy patch and select New From Clipboard in Max.

lh

tedthetrumpet's icon

That's nice, thanks, that's kind of the solution I was stumbling towards. Can't help thinking there must be a much simpler answer, though.

mudang's icon
Max Patch
Copy patch and select New From Clipboard in Max.

maybe somthing like this?

the_man361's icon

you could probably do some conditional processing based on bit masking, but im not totally sure on the best way to do this based on the data you have here..

also depends if you'd like to leave room for your system to grow or not, as if you only need it to handle 8 bars and it'll be like this always I'd probably use [% 8] and [sel 2 4 6 8] on the bar number to trigger your patches behaviour, using the last outlet for all other.