views:

342

answers:

3

Hi

I have a code like

A = B|C|D|E;

Throwing the warning "suggest parentheses around arithmetic in operand of |"

Expecting that expression needs high priority paranthesis for operators, tried the following ways:

A=(B|C)|(D|E);

one more as :

A=(((B|C)|D)|E);

Still the same warning persists.

Please help me in resolving this.

Thanks, Sujatha

B, C,D are enums and E is an integer.

+5  A: 

You have some arithmetic operator in your expression that isn't really simply B, or that isn't really simply C, etc. The compiler is suggesting that you parenthesize whichever expression so that readers will see that you wrote what you meant. If you don't parenthesize, everyone has to remember exactly what the priorities are, and they have to figure out if you remembered when you wrote it.

Try this: (B)|(C)|(D)|(E).

Windows programmer
B, C, D are enums and E is an integer
Programmer
I think you'd better do what greyfade suggested, run your source file through gcc -E. One of your enums might accidentally be a macro that you didn't know about.
Windows programmer
+2  A: 

Jordan is right even with -Wextra is there no warning

hereMan
+1  A: 

This is a weird warning. You only really need to pay attention to precedence when you're using different operators and those operators have different precedences. For instance, in arithmetic multiplication has higher precedence than addition.

But in this case you're using only one operator multiple times. Bitwise or is associative and commutative ((A | B) | C == A | (B | C) and A | B == B | A) so there's really no reason for the warning.

Max Lybbert