Home AMX User Forum NetLinx Studio

Protocols that require tweaking at bit level

Thankfully most serial protocols are in English ASCII, but occasionally you come across the weirdest things. I think this is rather old fashioned actually.

I have one where you tweak individual bits within a byte to control different things. This byte is part of a much longer string. Just wondering the most elegant way. Say for example I have 8 toggling buttons controlling 8 bits in a byte. I could have a variable associated with each button. If button 1 were pressed, its variable would be 1, otherwise 0 if off. If button 2 were pressed, its variable would be at 2, otherwise 0, button 3 value of 4, button 4 value of 8 etc. Then you add all the variables up and that's my byte. Is this the best way or am I missing something?

How about the reverse - parsing a byte down to bit level for feedback...do I have to keep dividing by 2 and looking at remainders? Or something with the Bitwise operators?

Thanks

OP

Comments

  • A very conserative way to get the bit values may be the following:

    DEFINE_VARIABLE
    MY_BYTE = $3A
    INTEGER VAL_BIT0
    INTEGER VAL_BIT1
    INTEGER VAL_BIT2
    INTEGER VAL_BIT3
    INTEGER VAL_BIT4
    INTEGER VAL_BIT5
    INTEGER VAL_BIT6
    INTEGER VAL_BIT7


    DEFINE_START
    VAL_BIT7 = MY_BYTE BAND $80 // result is 0
    VAL_BIT6 = MY_BYTE BAND $40 // result is 0
    VAL_BIT5 = MY_BYTE BAND $20 // result is $20 (32)
    VAL_BIT4 = MY_BYTE BAND $10 // result is $10 (16)
    VAL_BIT3 = MY_BYTE BAND $08 // result is $08 (8)
    VAL_BIT2 = MY_BYTE BAND $04 // result is 0
    VAL_BIT1 = MY_BYTE BAND $02 // result is $02
    VAL_BIT0 = MY_BYTE BAND $01 // result is 0

    So in this way, if a bit is set, the result is >0, and 0 if the bit is not set.

    Of course this can be done in some other and/or automated way. The above is just to show.
  • DHawthorneDHawthorne Posts: 4,584
    It is a bit old-fashioned to do things on a bitwise basis, but it can be more efficient when the code is running. The real question you have to ask yourself is whether the efficiency is worth the extra work coding it and the readability of the final product. For most systems, with the power of today's processors, the answer is no. The overhead of breaking it out into seperate variables or arrays is more than negligible in most cases.

    Back-in-the-day, when I was writing command-line DOS .com programs for a 640K environment, every bit counted. I used the method Marc illustrated - masks and bitwise comparisons. For the NetLinx environment, I just haven't seen a case where it is necessary. I have, however, run into some serial devices that work on that basis, so it is still a good technique to know.
  • Chip MoodyChip Moody Posts: 727
    Bwahahahaha. Been working with a device that returns "UTF-8" unicode strings. This format can have one printable character represented by up to four bytes. A character code up to $7F stays one byte. $80 through $7FF gets encoded into two bytes. $800 through $FFFF goes into three. $10000 through $1FFFFF goes into four.

    So I wrote a parser. For G3 panels, it weeds out anything outside of the single-byte ASCII range and replaces it with a "I can't show this symbol"* ASCII character. For G4 panels it was even more fun, translating from UTF-8 unicode to AMX's version of unicode.

    Long story short, (ooops - too late) lots of bit shifting and bit masking going on... :)

    - Chip



    * - user defined...
  • Just simple devices like a Kodak Ektapro slide projector uses bitwise instructions..... :-)
Sign In or Register to comment.