Page 1 of 1

Inverting a Bool

Posted: Sun May 24, 2020 11:24 pm
by ncc1502
Hi,

In a program I have to invert a Bool variable.

I did not want to use If A = 0 then 1 else 0, but just toggle the value:
In other C dialects it would be: A = !A ( where ! means NOT)
Since there does not seem to be a "!" in Flowcode, I used NOT

So I thought A = NOT A would do the trick, but then the varaible changes from 0 to 1, but never goes back to 0 again.



The method A = A XOR 1 works fine

Can somebody explain what happens when I use A = NOT A

Re: Inverting a Bool

Posted: Mon May 25, 2020 8:54 am
by mnf
Try using ~ (tilde)

Code: Select all

a = ~a
Where a is of type bool.

Should work

Martin

Re: Inverting a Bool

Posted: Tue May 26, 2020 12:24 pm
by medelec35
Here is my take on this.
There are two types of operators Bitwise and logic.
Bitwise (NOT, AND OR XOR ~ etc) is uses for bit manipulation and testing, not for comparing or toggling variables.
E.g if bool is required to toggle from 0 to 1 and back to 0

Code: Select all

a = ~a
or

Code: Select all

a = NOTa 
Will simply not work if expecting 1 or 0.

Logic (! && || etc) is use for comparing whole variables therefore if using a bool the only way to toggle is

Code: Select all

a = !a


Bitwise and logic will all work within the simulator.
However as bool is manipulated using a byte, results will not be as expected within hardware if used bitwise for togging!
Results will be 0 or 255 even though a bool is used.
ncc1502 wrote: Since there does not seem to be a "!" in Flowcode, I used NOT
You just use the ! key on your PC keyboard and enter within a calculation box a = !a
Flowcode will accept ! && ~ || etc

Re: Inverting a Bool

Posted: Tue May 26, 2020 1:48 pm
by mnf
Yes, sorry my bad...

~ does give the expected results (0, 1) in the simulator if variable is of type bool..
But - it is a bitwise complement - so if the hardware is treating it as a byte (then it will be 0 - 255 or worse 1 - 254) on hardware...
So to avoid surprises use !


Martin