Precedence of C # operators

Source: Internet
Author: User
Tags bitwise operators

In C #, there are 38 commonly used characters, based on the characteristics of the operations they perform and their precedence, and for ease of memory, I classify them as seven levels: 1, cell operators, and parentheses. 2. Regular arithmetic operators. 3, the displacement operator. 4, comparison operator. 5, logical operator. 6, various assignment operators. 7, right-bit (suffix) unit operator.

1, in this level, there is + + 、--(as a prefix), (), +,-(as the unit operator) 、!、 ~. This level is all a unit operator, except for one of those special pairs with parentheses that change the precedence of any operation. This shows that in the definition expression, the precedence of those cell operators is high, possibly because they are directly acting on the operand. Only two cell operators have precedence over the first level, and they appear behind for special reasons.

2, in the general arithmetic operators, we often use the *,/,%, +,-, because they use more, so also ranked higher position.

3. This level is the two special bitwise operators,<< and &GT;&GT, which are the highest precedence in all two-element operators except for regular operators, possibly because this set of operators is calculated as a numeric value compared to comparison operators and logical operators.

4, comparison operators, including <, >, <=, >=, = =,! =, a total of six, here is a feature, less than precedence over.

5, logical operators, there are four logical operators, but the "non" operator for the unit is in the front, so there is only &, ^, |, plus two additional operators to improve the efficiency of the code &&, | |, a total of five.

6, the assignment operator, this level is the most, almost before the two-dollar operator, where the addition of a "=" sign becomes an assignment operator. First of all, of course the most basic assignment operator "=", and then the general arithmetic operators evolved "*=,/=,%=, + =," which are in the same order as regular arithmetic characters, followed by the shift and logical operators, and in the order they were before they evolved, "<<=, > >=, &=, ^=, |= ". Because the comparison operator produces a result whose data type differs from the data type of its operand, they do not have a corresponding assignment operator.

7, the last level, is the two last unit operator suffix version of the + + 、--。 They appear to be a supplement to the two prefix versions of the = = 、--(The two priorities are too high and people need two less priority) to design the expression so that the two priorities are, of course, at the end of the line.

The order of precedence for these 38 operators can then be written as follows: (whether horizontal or vertical, the higher the precedence)
First level: + + 、--(as a prefix), (), +,-(as a unit operator) 、!、 ~.
Second level: *,/,%, + 、-。
Third level:<<, >>.
Fourth level:<, >, <=, >=, = =,! =.
Fifth level:&, ^, |, &&, | |.
Level sixth: =, *=,/=,%=, + =,-=, <<=, >>=, &=, ^=, |=.
Level seventh: + + 、--(suffix).

Precedence of C # operators

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.