Hello, community!
Firstly, I post this message on Visual Micro support forum, but they redirected me here.
I have noticed a strange behavior of the conditional operator “?” while working with bool variables.
First of all, the bool variable was initialized incorrectly. I restored it from an uninitialized EEPROM, so the variable contained 0xff instead of 0 or 1.
The attached program just demonstrates the issue.
The program was compiled for teensy 4.0 and teensy 4.1 and show the same results for both boards.
Here is the list of the program with comments:
And here is a result from terminal:
As you can see, the Bool_Var changes only its last bit and as a result, keeps the “true” value. Moreover, incorrect values are assigned to the “temp” variable, which, in principle, should not happen!
I know, there are many ways to get around this problem, but as I know everything should work as it is!
Of course, it doesn't bother me much, but I wonder if anybody met this behavior? Is it really a glitch or I don't see something?
Firstly, I post this message on Visual Micro support forum, but they redirected me here.
I have noticed a strange behavior of the conditional operator “?” while working with bool variables.
First of all, the bool variable was initialized incorrectly. I restored it from an uninitialized EEPROM, so the variable contained 0xff instead of 0 or 1.
The attached program just demonstrates the issue.
The program was compiled for teensy 4.0 and teensy 4.1 and show the same results for both boards.
Here is the list of the program with comments:
Code:
bool Bool_var;
uint8_t temp = 0x7f;
uint8_t Key = 0;
uint8_t OldKey = 0;
void setup() {
Serial.begin(115200);
while (!Serial) { ; }
*(uint8_t*)&Bool_var = 0xff; // initially here was restoring a variables from EEPROM, which wasn't initialized!
pinMode(0, INPUT_PULLUP); // hardware button connected to pin 0
}
// the loop function runs over and over again until power down or reset
void loop() {
Key = digitalReadFast(0); // reading the hardware button and try to invert the bool variable on each pressing
if ((Key == 0) && (OldKey == 1)) { //
//Bool_var = Bool_var ? false : true; // initially i saw issue in this expression, but during the studying
// of the problem, I changed the code to one that is not commented out.
temp = Bool_var ? 0 : 1; // if I change this expression, to use other values, not just 0 and 1,
// everything starts to work perfectly.
//temp = Bool_var ? 0 : 5; // For example, this expression works fine.
Bool_var = !Bool_var;
//Bool_var = (bool)temp; // this string gives the same result as previous string
Serial.printf("temp=%i; Bool_Var=%i; ", temp, Bool_var); // output results to terminal
if (Bool_var) Serial.printf("Bool_Var=true;\n");
else Serial.printf("Bool_Var=false;\n");
}
Code:
Opening port Port open
temp=254; Bool_Var=254; Bool_Var=true;
temp=255; Bool_Var=255; Bool_Var=true;
temp=254; Bool_Var=254; Bool_Var=true;
temp=255; Bool_Var=255; Bool_Var=true;
temp=254; Bool_Var=254; Bool_Var=true;
temp=255; Bool_Var=255; Bool_Var=true;
I know, there are many ways to get around this problem, but as I know everything should work as it is!
Of course, it doesn't bother me much, but I wonder if anybody met this behavior? Is it really a glitch or I don't see something?