News:

Let's find out together what makes a PIC Tick!

Main Menu

Odd behaviour on "~"

Started by Trikkitt, Sep 27, 2025, 03:58 PM

Previous topic - Next topic

Trikkitt

I'm decoding an incoming message, the protocol is old and out of my control.  There is one important number contained in the message stored in a byte array, first it is sent true and then sent as 1s complement.  In my code I used the line:
If RX1Buffer[1]= ~ RX1Buffer[2] Then
However this always failed validation. So I re-wrote it to XOR the two numbers together which should result in all bits set:
RXdecode=RX1Buffer[1] ^ RX1Buffer[2]
If RXdecode=255 Then

The use of XOR works perfectly.  Any idea what I'm doing wrong with the ~ operator?  Does it just not work within an IF statement?

I'm using the latest compiler version posted on here.



RGV250

#1
Hi,
Have you tried to use a temporary variable for the inverted value before the compare. I have found a few times that things do not always work with inline commands like that.
Something like
Temp = ~RX1Buffer[2]
if RX1buffer[1] = Temp then

Bob

Trikkitt

Quote from: RGV250 on Sep 27, 2025, 04:55 PMHave you tried to use a temporary variable for the inverted value before the compare. I have found a few times that things do not always work with inline commands like that.

Thanks! I haven't but I have a feeling it must be the inline part of the IF statement that is the issue.  I've used ~ elsewhere in the code but always straight to variables and those are working just fine.  It'd be nice if the compiler told you it couldn't do something, because it took quite a while to track this issue down.

Pepe

you must write
If RX1Buffer[1]= ~RX1Buffer[2] Then

not
If RX1Buffer[1]= ~ RX1Buffer[2] Then

RGV250

Hi,
QuoteIf RX1Buffer[1]= ~RX1Buffer[2] Then
Interesting that the space causes different code where all the other logical operators can have a space. Perhaps if this is the case then the compiler should issue a warning / error that there is a space after the ~

Bob