Bitwise Manipulation Affecting Variable Selection

This is a mid-point in my processing. Apologies, but I think it would be terribly cumbersome to try to document my inputs and outputs with enough explanation to make the big picture palatable.

It is insane. Trust me. heh heh

However, this is where I'm at with a certain Preset which is a first step in a much larger world /Ben Kenobi.

I need to select one of four of variables based on which bit is "1" in the first 4 bits of another variable.

Right now, g0 is the index. I'm using the first 4 bits to represent a toggle of 4 buttons. The bits of code before this ensure that only one of the 4 bits is set to "1" (or what I'm sometimes terming 'On').

If the first bit is the 1, then I want to put HA in the destination. If the second bit is the 1, then I want to put HB in the destination.

Third, HC; Fourth, HD.

HA-HD are all 'masks' that represent the On/Off states for 16 different destinations (incidentally, a 0 is 'on' or that it is not masked). I'm attempting to use the bit manipulation technique to toggle the buttons and select the appropriate mask, which is then stuck in GA which is used later on to compare to another mask when output is triggered.

What I have right now is something like:

Incoming: 87 pp [any velocity]

// Have a bunch of buttons and I test for just the 4 in the first column in a specific range, because: reasons

if pp<48, then exit/skip

if pp>63 then exit/skip

ww=pp%4

if ww!=0 then exit/skip

// So now I've narrowed it down to its button (note) 48, 52, 56, or 60. Let's figure out an index:

xx=pp-48

vv=xx/4

//vv is now the index (0,1,2,3) for the bit manipulation

uu=1<<vv

g0=g0^uu

g0=g0&uu

//g0 is the variable to keep track of which button is on; it is exclusive, so only one button is 'on' at a time (the last line clears out any previous 'on' states); also: buttons can be all off (0b0000)

//The only way I can wrap my head around:

GA=0

oo=g0>>0

oo=oo&1

qq=oo*HA

GA=GA|qq

//--

oo=g0>>1

oo=oo&1

qq=oo*HB

GA=GA|qq

//--

oo=g0>>2

oo=oo&1

qq=oo*HC

GA=GA|qq

//--

oo=g0>>3

oo=oo&1

qq=oo*HD

GA=GA|qq

--

I could use a timer, but this is not the 'end' of the processing path. Moreso, I have a timer for the final output of this whole craziness. My understanding is if I stick a timer in there to cycle through these 4 values it will trigger the Outgoing from the timer 4x and that's not quite necessary or warranted.

I'm okay with the above, but I wonder if there's a more elegant way to do it that might encompass more scenarios and allow me to use the same code across other button groupings.

I'm not that great wrapping my head around Timers wrt parallel processing, so I'm probably missing something terribly obvious. If I had more than 32 bits to play with, I'd use one super-mask and then index from there (0-15 for the first, 16-31 for the second, etc)

g0=4 bit toggles for the button states for 4 buttons GA = destination for which of the 4 'masks' will be considered current HA, HB, HC, HD = 'masks' for 16 destination outputs, using the first 16 bits where a "1" indicates that a given destination will be played or muted (Note: 1="Muted", so a mask of value 5 means that Destination 1 and 3 are muted, all the rest are played)

pp=Inc Note #, test to be 48-60, and that it is evenly divisible by 4 (which represents the first column of my button matrix)

ww=Tester variable to verify Note/Button in question is first column

xx=Resulting Note # after forced into range 0-12

vv=Derived from Note/Button#, ends up being the Index for the bit manipulation

oo=temp var for determining which mask goes into the 'Current' global variable position

qq=temp var to help calc which mask is selected

Hmm, maybe I’m over simplifying or missing something but couldn’t you just do this.

previous rules OK and excluded here

// clear other bits
g0=g0&uu
// Select mask
if g0==1 then ga=ha
if g0==2 then ga=hb
if g0==4 then ga=hc
if g0==8 then ga=hd

Steve Caldwell
Bome Q and A Moderator and
Independent Bome Consultant/Specialist
bome@sniz.biz

or maybe the below if you want to OR with existing ga value
if g0==1 then ga=ga|ha

Yeah, that’s pretty much what I’m doing except I go the longer way around.
I keep thinking in do/while loops and need to stop that. heh

Thanks, Steve.
It helps to know I’m not as ignorant as I assume I am. (Can i be aware I’m under the Dunning-Kruger effect and still be DK?!?) LOL

Note: Funny bit is that in other Presets where I’m defining the masks, I’m personally relating to variable representing the toggles as a single decimal value.

But then when I decide I want to test it IN the script, I forget that each mask combination is a different single decimal value and instead I go thru the whole “oooh, let’s test each bit” thing. LOL

So yeah: DK in the house. Thanks! You helped adjust my perspective on that and got me back on track.

You know, no matter how smart I think I am. Sometimes when working on a problem for a while I also find my self going around in circles on what is normal a simple problem. I think some times our minds want to overcomplicate things.

Some days I can go for hours on a problem that other days takes me 5 minutes.