Negation of boolean by “~” doesn't work with MCP-Compiler

Moderator: Benj

Post Reply
Nigel_K
Flowcode v5 User
Posts: 8
Joined: Thu Aug 29, 2013 3:43 pm
Has thanked: 4 times
Contact:

Negation of boolean by “~” doesn't work with MCP-Compiler

Post by Nigel_K »

The behaviour of the simulation is different to the one on the target when using a negation of a boolean variable foo (foo = ~foo). The C-code itself looks okay and works in the simulator but on the target the respective expression doesn't get evaluated properly...
As a workaround I avoided the “~” and it works now also on the target but I wanted to report that at least.
edit: I'm using the PIC24/dsPIC version of Flowcode

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Negation of boolean by “~” doesn't work with MCP-Compile

Post by kersing »

If you require a logical NOT (meaning everything not equal to zero becomes zero and zero becomes a non zero value) it is better to use the exclamation mark ('!'). Depending on the compiler the bitwise NOT (or two complement operator) creates unexpected results. Not 1 would be 0 if a boolean is stored as 1 bit, however for most compilers a boolean is stored in a byte, meaning not 0000 0001 becomes 1111 1110 which is <> 0.

The simulator should implement the reality as generated by the compiler, not the ideal world.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Nigel_K
Flowcode v5 User
Posts: 8
Joined: Thu Aug 29, 2013 3:43 pm
Has thanked: 4 times
Contact:

Re: Negation of boolean by “~” doesn't work with MCP-Compile

Post by Nigel_K »

Hi kersing, thanks for your fitting solution. I also tried "NOT" but didn't think on "!"... :oops:
It works well now as you said. :)
I just found time now to try every of your solutions (god bless the weekend). ;)

Post Reply