 |
 |
| View previous topic :: View next topic |
| Author |
Message |
Ttelmah Guest
|
|
Posted: Sat Jun 11, 2005 4:09 am |
|
|
I should have thought of the macro bhaviour problem, in the past on anothr thread where a poster was having problems with bit masking. (Go and slap myself...).
This is a classic problem, and really means that CCS have been careless with their defines.
Basically, if you define a value like:
#define mask(n) 1<<n
and then use this in a statement like:
result= mask(2) & val;
or
result = mask(2) * 2;
you are 'reliant' on the C rules of precedence. The resulting statement, in the first case, will be:
result = 1<<2 & val;
In this case, the '&' statement has a lower precedence than the '<<', and the statement will work as expected. However in the second case, the result:
result = 1<<2 * val;
will multiply 2 by val, and then use this number for the shift. This will give totally different results for what may be expected....
This is why you will see some posters being very 'heavy handed' with brackets in #define statements, which allow you to force the evaluation to be in the 'expected' order. so changing the definition to:
#define mask(n) (1<<n)
ensures the statement evaluates as expected when used in a compound statement.
How such 'mis-expansion', affects a particular statement, depends on the code used, but i'd possibly suggest that the 'input_b' statement, is internally more complex than you might think (remember this has to test the flags for the port 'mode', and control tris if needed), and is possibly ending up applying the mask somewhere incorrectly, in this process...).
Fun!...
At least you have a solution that works now.
Best Wishes |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|