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

Moderator: Benj

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

Postby Nigel_K » Thu Aug 29, 2013 7:24 pm

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: 1873
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 530 times
Been thanked: 1040 times
Contact:

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

Postby kersing » Thu Aug 29, 2013 9:10 pm

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.
These users thanked the author kersing for the post:
Nigel_K (Sat Aug 31, 2013 3:21 pm)
Rating: 5.56%
 
“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

Postby Nigel_K » Sat Aug 31, 2013 3:21 pm

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). ;)