Home AMX User Forum NetLinx Studio
Options

Bitwise or integer?

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

Comments

  • Options
    I don't know anything about the system you are talking about but i have used bitwise feedback a lot. just check the protocol how to use it properly.
    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.
  • Options
    Chip MoodyChip Moody Posts: 727
    I'm kinda with you on this one, Jeff. I use bitwise math a lot, but it's because I'm trying to do a CRC or encoding/decoding stuff on strings - not for maintaining data of my own creation/use inside a program.

    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
  • Options
    Actually what I've been doing lately is parsing out LED status from a keypad and then turning on channels for LED feedback on a Virtual device. So say I get a string back that indicates led 2,4,5 are on, I"ll go ahead and turn on channels 2,4,5 on my virtual keypad. This has worked awesomely, also I can check the state of a keypad based on the online tree and the port feedback.

    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.
  • Options
    Spire_JeffSpire_Jeff Posts: 1,917
    Dries,

    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
  • Options
    DHawthorneDHawthorne Posts: 4,584
    I simply use a CHAR array - something like:

    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.
  • Options
    frthomasfrthomas Posts: 176
    There are two cases where using bitmaps makes sense IMHO:
    - 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
  • Options
    Bitwise AND Not Working!

    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?
  • Options
    GSLogicGSLogic Posts: 562
    Binary values are the smallest unit of memory that can be manipulated. Having said that, there is no doubt that it will save you memory and processing speed. The issue is, is it worth it? If you're working with machine language binary values are a must, but in the platform we're working in, the difference is very small. I still use them when I can; it keeps me from get bored. :)
Sign In or Register to comment.