Page 1 of 1

Problem with var bool

Posted: Mon Mar 05, 2012 11:22 am
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 ?

Re: Problem with var bool

Posted: Mon Mar 05, 2012 12:45 pm
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

Re: Problem with var bool

Posted: Mon Mar 05, 2012 2:19 pm
by JCMB
Thank's for the quick answer.
The problem is that in simutation there is no difference !

Re: Problem with var bool

Posted: Mon Mar 05, 2012 2:46 pm
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

Re: Problem with var bool

Posted: Mon Mar 05, 2012 3:26 pm
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.

Re: Problem with var bool

Posted: Mon Mar 05, 2012 3:43 pm
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

Re: Problem with var bool

Posted: Mon Mar 05, 2012 4:11 pm
by JCMB
Thank's very much for your quick help and teach.