Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ warning: suggest parentheses around arithmetic in operand of |

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.

like image 636
Programmer Avatar asked Jun 04 '10 06:06

Programmer


2 Answers

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).

like image 106
Windows programmer Avatar answered Oct 07 '22 00:10

Windows programmer


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.

like image 31
Max Lybbert Avatar answered Oct 07 '22 00:10

Max Lybbert