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
Inverting a Bool
Moderator: Benj
-
- 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
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 or 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
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.
Flowcode will accept ! && ~ || etc
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
Code: Select all
a = NOTa
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.
You just use the ! key on your PC keyboard and enter within a calculation box a = !ancc1502 wrote: Since there does not seem to be a "!" in Flowcode, I used NOT
Flowcode will accept ! && ~ || etc
Martin
-
- Valued Contributor
- Posts: 1191
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
- Contact:
Re: Inverting a Bool
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
~ 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