Documentation request: operator precedence

I notice that the Arduino Reference, here:

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)

.

You can Google "C++ operator precedence" but I agree, a mention on the Reference page would be nice.

Here is what I am trying to understand:
The Reference page has information on these operators, among others: + - * / % = ==
I see that you explain the use of these operators, rather than assuming that we are familiar with them from having used C++. Also, you volunteer this explanation, rather than simply referring us to a (non-Arduino-specific) C++ reference.
What I don't understand is a philosophy that reckons it worthwhile to explain the use of the operators, but considers it less than obligatory to even mention that these operators have an order of precedence, much less detail what that order is, exactly.

odometer:

[quote author=Nick Gammon link=topic=111009.msg834806#msg834806 date=1340313571]
You can Google "C++ operator precedence" but I agree, a mention on the Reference page would be nice.

Here is what I am trying to understand:
The Reference page has information on these operators, among others: + - * / % = ==
I see that you explain the use of these operators, rather than assuming that we are familiar with them from having used C++. Also, you volunteer this explanation, rather than simply referring us to a (non-Arduino-specific) C++ reference.
What I don't understand is a philosophy that reckons it worthwhile to explain the use of the operators, but considers it less than obligatory to even mention that these operators have an order of precedence, much less detail what that order is, exactly.
[/quote]

The order of most operations is the same as normal (PEMDAS), so it's intuitive for those new to arduino. Knowing that the language has them is not. Once you get into bitwise arithmetic you're advanced enough to know where to look it up.

Hi

Just to keep you and anyone you want to share your code with sane, you can use the ( ) as you did to illustrate your confusion in the first post, it doesn't generate any additional code and ensures you get the results you wanted.

Duane B

I double DuaneB,

use as much () as possible or use "simple C notations" by using additional variables.

e.g if ((x&15)>9)....

becomes

t = x & 15;
if (t > 9)

(x&15>9) for intended

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

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.