Page 1 of 1

Get Boolean from Byte

Posted: Thu Dec 19, 2013 11:30 am
by Jan Lichtenbelt
I have 2 variables: A as boolean (1 bit, Flowcode 6) and B as byte (8 bits).

I want to put the LSB of B into A. Best best way is A= B & 0x01. But I have lack on memory. Thus my question is if A=B is enough? Or would that give unexpectable results?

Kind regards

Jan Lichtenbelt

Re: Get Boolean from Byte

Posted: Thu Dec 19, 2013 12:45 pm
by Benj
Hi Jan,

It depends on how good the compiler is and if there are other boolean variables present in the program that it can combine together to make up a byte. The device can only store or process 8-bit values so it is likely that there may be complications in some cases when your not using the and operation. A standard GCC compiler should cope ok but will probably be adding the and operation behind the scenes plus more code to shift the bool val to the correct location in the storage byte.

The best way is probably to do some testing with some corner case situations and see what you get on the hardware. In honesty I hardly even use bool variables because of these complications, they are not worth the hassle and uncertainty. I use bytes and then if I need flags in the byte then I can control them myself without hoping the compiler will provide the additional optimisation.