Prioritization and understanding of the 38 commonly used operators in C

  • 2020-05-07 20:15:16
  • OfStack

In C#, 1 has a total of 38 commonly used operators, which I classify into seven levels for memorization based on the characteristics of the operations they perform and their precedence: 1, unit operators, and parentheses. 2. Regular arithmetic operators. 3. Displacement operator. 4. Comparison operator. 5. Logical operators. 6. Various assignment operators. 7. Right bit (suffix) cell operator.

1, in this level, there are ++, --(as a prefix), (), +, -(as a unit operator),! And ~. All of this 1 level is a unit operator, except for the special pair of parentheses that change the priority of any operation. As you can see, those unit operators have a high priority in defining expressions, probably because they all operate directly on operands. Only two of the cell operators have a priority that is not in level 1, and they appear later for special reasons.

2, in the regular arithmetic operators, we often use *, /, %, +, -, because they are used more, they are also ranked higher.

3. This level 1 is the two special bit operators, < < and > > , they are the highest priority of all 2-bit operators other than the regular operators, probably because the 1 set of operators is still performing numerical calculations compared to the comparison operators and logical operators.

4. Comparison operators, including < , > , < =, > =, ==,! Is equal to 1, 6 of them, 1 of them is less than than greater than.

5, logical operators, logical operators originally there are four, but for the unit of the "not" operators ranked first, so there are only &, ^, |, plus two supplementary to improve the code efficiency of the operator &&, ||, a total of five.

6, the assignment operator, this level is the most, almost the previous 2 operators, put a "=" sign here to become an assignment operator. First of all, of course, the most basic assignment operator, "="; Then came the "*=, /=, %=, +=, -=", which evolved from the regular arithmetic operators in the same order as the regular arithmetic operators. Then the displacement operator and the logical operator, also in their pre-evolution order, are" < < =, > > =, &=, ^=, |= ". Because the data type of the result produced by the comparison operator is different from the data type of its operands, they have no corresponding assignment operator.

7. The last level is the ++, -- of the suffix version of the two last unit operators. They are there to complement the two prefix versions of ==, -- (which are too high priority, people need two less high priority) to facilitate the design of the expression, so they are of course at the bottom of the list.

The order of precedence of the 38 operators can then be written as follows :(horizontal and vertical, the higher the priority)
Level 1: ++, --(as a prefix), (), +, -(as a unit operator),! And ~.
Level 2: *, /, %, +, -.
Level 3: < < , > > .
Stage 4: < , > , < =, > =, ==,! =.
Level 5: &, ^, |, &&, |, |.
Level 6: =, *=, /=, %=, +=, -=, < < =, > > =, &=, ^=, |=.
Level 7: ++, --(as suffix).

This is a summary of my study of C# 1, in which I do not have a correct understanding of the classification of the levels and the priority of each operator, but my own views, to share with you.

Related articles: