odometer:
I notice that the Arduino Reference, here:
Arduino - Home
does not seem to contain any information about operator precedence (at least, not where I could find it easily).
Not knowing about operator precedence led me to make huge bugs.
For example, I used
(x&15>9)
for intended
((x&15)>9)
.
This is a place where using the IDE isolates you from the underlying compiler. GCC actually has a -Wparentheses that warns about this:
unsigned long foo (unsigned long x)
{
return (x&15>9);
}
On my Linux system g++ -O2 -Wparentheses gives:
foo.cc: In function ‘long unsigned int foo(long unsigned int)’:
foo.cc:3:16: warning: suggest parentheses around comparison in operand of ‘&’
DuaneB:
or use "simple C notations" by using additional variables
Even better, especially for those occasions when ((x&15)>9) doesn't do what you expected and needs to be debugged.
I assume that the compiler optimises out all of the temporary variables anyway so the resulting upload is the same whether you can read it or not in which case there is no justification for writing condensed code.
Anyone going to volunteer to test the optimisation ?
Duane B
Assuming the IDE actually passes an optimization option to the compiler, such as -O or -O2, I can state for non-fortran code extra parenthesis won't matter in general. For Fortran, the language standard actually states that the compiler is not free to reorder expressions with an explicit parenthesis (it matters in some corner cases involving floating point), and modern versions of GCC now have options that say it is ok to rearrange things for speed.
Now, I've been working on GCC for at least 21 years now (with a small break when I was working on other compilers), and even back in the 1.37 era when I joined the project, it would do that level of optimization, assuming you pass at least -O when compiling. Note, I don't work on the AVR side of things, my day job is the care and feeding of the powerpc backend. I haven't even looked at what the instruction set of the AVR is, and I don't particularly want to, since my Arduino stuff is a hobby.