Inverting a Bool

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
ncc1502
Posts: 48
Joined: Mon Apr 07, 2008 9:18 pm
Has thanked: 23 times
Been thanked: 8 times
Contact:

Inverting a Bool

Post 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
Better safe then sorry

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Inverting a Bool

Post by mnf »

Try using ~ (tilde)

Code: Select all

a = ~a
Where a is of type bool.

Should work

Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Inverting a Bool

Post 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
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Inverting a Bool

Post 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

Post Reply