Results 1 to 2 of 2

Thread: Defined Behavior of Bits in Arithmetic and Conditionals

  1. #1

    Defined Behavior of Bits in Arithmetic and Conditionals

    Hello All,

    I have spent some time digging around the compiler manual and online, but I can seem to find a clear answer for the behavior of the following code:

    myBit1 var bit
    myBit2 var bit
    myByte var byte

    myBit1 = 1
    myBit2 = 1

    myByte = 0

    if myBit1 + myBit2 = 2 then
    //Will this evaluate to true?
    endif

    //How about this?
    myByte = myBit1 + myBit2

    //Would either of this change if they were aliases for pins instead of bit vars?

    I can't seem to find any explanation in the compiler manual or otherwise how PBP 3 handles these sorts of operations. If I can add multiple bits together and get a byte, it would allow me to write much more simple code in cases where I need to compare the state of multiple pins or flags or combinations of the two. I'm aware I could use bitwise operations or other comparisons to do the same, but being able to leverage math would be useful at some points.

    The trouble is, in my testing it seems to work okay in some cases, and not in others. Leading me to think it's not really handled in a consistent way by the compiler (or simply that I made some other mistake).

    If anyone could clear this up for me, it would be extremely appreciated!

    Thank you!
    Matt

  2. #2
    PBP's math operators will always use the numerical value of inputs, regardless of the variable type. The additions you wrote should work as you expect. 1 + 1 = 2, regardless of whether the inputs are bits, bytes, or words.

    Bitwise operators are a different animal. When using symbols like & and |, it very much matters what the input variable types are.

    PBP treats pins as bit-variables.

    I have seen a few cases where PBP handles hierarchy-of-operations in an unexpected way. I don't think you would encounter this in the IF statement you wrote, but parentheses are always a good idea to help PBP sort things out.

    if (myBit1 + myBit2) = 2 then

    If you can post some actual code that I can use to reproduce behavior, I'll look into it.
    Charles Leo
    ME Labs, Inc.
    http://melabs.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •