Frequently Asked Questions
Why does the generated code that uses BIT variables look so ugly?
Bit variables (SHORT INT) are great for both saving RAM and for speed but only when used correctly. Consider the following:int x,y; short int bx, by; x=5; y=10; bx=0; by=1; x = (x+by)-bx*by+(y-by);
When used with arithmetic operators (+ and - above), the BX and BY will be first converted to a byte internally: this is ugly. If this must be done, you can save space and time by first converting the bit to byte only once and saving the compiler from doing it again and again. For example:
z=by; x = (x+z)-bx*z+(y-z);
Better, would be to avoid using bits in these kinds of expressions. Almost always, they can be rewritten more efficiently using IF statements to test the bit variables. You can make assignments to bits, use them in IFs and use the &&, || and ! operators very efficiently. The following will be implemented with great efficiency:
if (by || (bx && bz) || !bw) z=0;
Remember to use ! not ~, && not & and || not | with bits. Note that the INPUT(...) function and some other built-in functions that return a bit follow the same rules.
For example do the following:
if ( !input( PIN_B0 ) )
NOT:
if( input( PIN_B0 ) == 0)Both will work but the first one is implemented with one bit test instruction and the second one does a conversion to a byte and a comparison to zero.