Page 1 of 1

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

Posted: Thu Aug 29, 2013 7:24 pm
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

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

Posted: Thu Aug 29, 2013 9:10 pm
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.

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

Posted: Sat Aug 31, 2013 3:21 pm
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). ;)