Bitwise or integer?
Spire_Jeff
Posts: 1,917
I was just sitting here thinking about something I believe I read here about using bitwise flags to indicate LED status in regards to a Lutron system. As I thought about it, one main question comes to mind: Should I bother with bitwise indicators?
Personally, on a Lutron system I use integers to indicate the button status. I then have an array setup as such: nKP_STATUS[num_keypads][24]. This allows me to easily place the current status of a button in the array (There are 4 possibilities: 0=off, 1=on, 2=Flash 1, 3=Flash 2) Currently I am just reporting on or off (flash=on), but I do have the ability to treat each button according to the actual state if I choose to and I don't have to do any of that funky bitwise manipulation to see the current status of a button. I know this may use up more memory, but in this case, I feel the usability factor out weighs the memory use. (64 keypads done this way would only use 3072 bytes of memory if my math is correct)
The place where I do use bitwise operations is for grouping. I have an array of a room structure and one of the elements of the room structure is group membership. Now because a room might belong to more than one group (ie Bedrooms & Main Floor), I chose to go with bitwise flags because it seemed the easiest way to indicate all memberships and test for membership in a single group.
What I am wondering is, how do other people use bitwise operations and is there something I am not considering or maybe not considering properly with regards to the bitwise/integer descision?
Jeff
Personally, on a Lutron system I use integers to indicate the button status. I then have an array setup as such: nKP_STATUS[num_keypads][24]. This allows me to easily place the current status of a button in the array (There are 4 possibilities: 0=off, 1=on, 2=Flash 1, 3=Flash 2) Currently I am just reporting on or off (flash=on), but I do have the ability to treat each button according to the actual state if I choose to and I don't have to do any of that funky bitwise manipulation to see the current status of a button. I know this may use up more memory, but in this case, I feel the usability factor out weighs the memory use. (64 keypads done this way would only use 3072 bytes of memory if my math is correct)
The place where I do use bitwise operations is for grouping. I have an array of a room structure and one of the elements of the room structure is group membership. Now because a room might belong to more than one group (ie Bedrooms & Main Floor), I chose to go with bitwise flags because it seemed the easiest way to indicate all memberships and test for membership in a single group.
What I am wondering is, how do other people use bitwise operations and is there something I am not considering or maybe not considering properly with regards to the bitwise/integer descision?
Jeff
0
Comments
example; if a device returns an 8 bit value where bit 6 is the flag for indicating whether the device (dvd player) is in the playing state just do a binary AND (nValue BAND $20) . if the condition is true it is flagged.
If the device you are controlling has no other input devices other than AMX (switches or other remotes) than real time feedback has no real advantage over setting variables (or channels) in your code IMO.
I did try incorporating that into a program once. I was trying to be clever by saving variables by taking a bunch of switcher routing values that would never go higher than 32 and using the upper bits to keep track of other on/off, yes/no type info.
Great conceptually, but going back later to debug stuff was one of the biggest pains in the tukkas...
Since then, unless there's a REALLY good reason not to, everything I'm trying to keep track of gets its own darn variable. Makes revisiting and "getting back into my own head" at a later date SO much easier...
- Chip
Then I also parse out KPD addresses and do a DO_PUSH(VDVkpd,51) for say a press, and DO_RELEASE(VDVkpd,51) on the release string. This allows me to use a Lutron keypad just like any old touchpanel in AMX. Also I'll DO_PUSH(VDVkpd,151) for say like a double tap from lutron. Been working awesome.
I am refering to the use of bitwise data storage in code I am writing. I have been fortunate in that I haven't had to deal too much with devices sending bitwise encoded data.
Chip,
That's pretty much where I was heading with my thought process. I did find that bitwise group representation wasn't too bad because once I got it working, I haven't had to revisit it....yet
Jeff
CHAR cLED_State[NUMBER_KP][24]
On the KLS string event, I parse to seperate the data portion, and just dump it directly in my array. Since these are ASCII values, in my feedback section I just iterate through the array and a second iteration through the LED state string, doing either a direct comparison or using ATOI, dependiong on the application. Rarely do I worry about flashing LED feedback, so a simple [dvTP, BUTTON] = ATOI("cLEDState[KP_NUM][BUTTON_NUM]") suffices.
- When you have 20 or more binary states to track, since bitmaps save a lot of space.
- When you need set operations, like substraction, intersection, union and the like.
Anything else and you're better off using channels or just char arrays or some other technique.
Fred
I have tried using bitwise manipulation to save memory - worked well on Axcess platforms but not on Netlinx. Somebody (can't remember who) said not all the Bitwise operators (I think it was BAND) worked correctly in Netlinx. Does this ring any bells with anybody? Has it been fixed?