Problem with var bool

Moderator: Benj

Post Reply
JCMB
Posts: 22
Joined: Wed Feb 08, 2006 3:15 pm
Location: St Etienne, France
Been thanked: 1 time
Contact:

Problem with var bool

Post by JCMB »

With the attached code if Debug =0 Leds are always ON, if Debug = 1 they are blinking.
Debug = 0 use var bool, Debug = 1 use Int.
Doe's my coding is frong in using var bool ?
Attachments
VBoolLed.fcf
(16 KiB) Downloaded 326 times

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Problem with var bool

Post by JonnyW »

Hello.

If you change:

Code: Select all

LedBool0 = NOT LedBool0
LedBool1 = NOT LedBool1
To:

Code: Select all

LedBool0 = !LedBool0
LedBool1 = !LedBool1
This works. The '!' is a logical-not, and 'NOT' is bitwise. In C, both these operations should work, but I believe BoostC treats 'bool' as C++ would.

For curiosty, this is why it does not work:
  • The LedBool0 variable has a value '1'
  • The compiler takes the 1 bit LedBool0 and converts to a byte, 0x01
  • The byte is inverted, so is 0xFE
  • A Boolean conversion is performed to write back to LedBool0 - 0xFE is non-zero so is converted to '1'
  • The value is now written back, but unchanged
As a general rule, I would use logical operations (!, &&, ||) when dealing with Boolean values, instead of the bitwise (~, &, |) which work on integers.

Jonny

JCMB
Posts: 22
Joined: Wed Feb 08, 2006 3:15 pm
Location: St Etienne, France
Been thanked: 1 time
Contact:

Re: Problem with var bool

Post by JCMB »

Thank's for the quick answer.
The problem is that in simutation there is no difference !

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Problem with var bool

Post by JonnyW »

No, the simulation behaves as C would. If you used a different ANSI-C compliant compiler like Hi-Tec this would behave as simulation does. In future versions it would be nice to provide a "Simulate 'bool' as C++" option in the project or global settings and provide warnings on compilation or simulation but until then I would say just use logical operators with Boolean values and you will be OK.

Cheers,

Jonny

JCMB
Posts: 22
Joined: Wed Feb 08, 2006 3:15 pm
Location: St Etienne, France
Been thanked: 1 time
Contact:

Re: Problem with var bool

Post by JCMB »

Sorry if J don't understand your answere.
J use for compiling boostc delivered with FlowCode.
In FlowCode simulation leds are blinking with Debug = 0 or Debug = 1.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Problem with var bool

Post by JonnyW »

Hi. Thats OK - Flowcode is shipped with BoostC as its compiler, but can use other C compilers if you wish to change this.

Because Flowcode compiles to C Flowcode simulates as C code would. But BoostC 'bool' values behave like they would in C++, which is subtly different.

As a general rule, only use the logical operators that are designed to work with Boolean values (don't use 'NOT', use '!') as Boolean is not an arithmetic type, in the same way that strings are not arithmetic types. Also, try not to mix types - adding a bool to a float for example may be allowed, but does not make much sense.

For reference, here are the types supported in Flowcode and the operators you should use on them:
bool
!, ||, &&, ==, !=, |, &, ^
(Be careful using |, &, ^ and don't mix bools with ints)
int, uint, etc
All operators
float
All operators except: &, |, ^, ~, <<, >>
string
+ (String concatenation only)

I hope this is more clear, and I have not complicated things further!

Jonny

JCMB
Posts: 22
Joined: Wed Feb 08, 2006 3:15 pm
Location: St Etienne, France
Been thanked: 1 time
Contact:

Re: Problem with var bool

Post by JCMB »

Thank's very much for your quick help and teach.

Post Reply