Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do shifts have lower precedence than addition and subtraction in C?

I sometimes find this inconvenient when doing bit manipulation (though I can't recall to mind any specific examples right now). I also find it conceptually confusing, since shifts are basically multiplication and division by powers of two.

I see that it can be convenient in C++, when using << to send output to an ostream, but of course that can't be used to explain how the order was originally fixed in C.

like image 664
Brian Bi Avatar asked Feb 21 '12 01:02

Brian Bi


People also ask

Which operator has the lowest precedence in C?

The table below lists the C language operators in order of precedence and shows the direction of associativity for each operator. The primary operators have the highest precedence. The comma operator has the lowest precedence.

Which operator is having the lowest precedence?

LOWEST PRECEDENCE The compound logical operators, &&, ||, -a, and -o have low precedence. The order of evaluation of equal-precedence operators is usually left-to-right.

Which operator has the highest precedence in C?

Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator. For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.


1 Answers

Because that's what the authors of the C language decided.

Use parentheses to avoid confusion.

like image 154
Oliver Charlesworth Avatar answered Oct 22 '22 13:10

Oliver Charlesworth