Pit operator, pit Operator

Source: Internet
Author: User

Pit operator, pit Operator

 

I. Operator priority

Two days ago, I saw an essay on the garden's homepage. I couldn't find the essay address (I'm sorry), but I still remember one or two of the code snippets, probably as follows:

            Thread t = null;            string message = "I'm ..." + t == null ? "And ..." : "";            Console.WriteLine(message);

There are few codes, but I think many people will do it. The most critical issue is the priority of operators. Here is the C # reference operator. Based on the priority order, I have summarized the following rules:

Maximum:. (point) () [] I ++ I --

Second, the single object operator (I ++ higher than ++ I)

Then: Calculate the multiplication and division, and then calculate the addition and subtraction, the final displacement, and the ratio (greater than or less than equal to or equal)

Next: Not And Or (that is! , &, |)

Final: "three items", "value assignment", and "Lambda"

This priority order already contains most operators. As long as it is not a very abnormal problem, I think it can be done.

 

Ii. Greedy Compiler Principles

This topic is derived from in-depth analysis of Java-36 essential Java topics. (Although it originated from Java topics, it also applies to C # And has been verified)

How do I calculate the three plus signs? The code snippet is as follows:

Int I = 10; int j = 20; int k = I ++ j; // the code is not formatted, but the Console can be compiled and run normally. writeLine (k); // What is the output result? Is it 30 or 31?

First, we will not look at the answer. First, we will format the code and see the result:

Int k = I ++ j; // Why is the formatted result not formatted as I ++ j? The answer is: the greedy principle of the compiler.

Greedy principle: the compiler combines as many valid symbols as possible when analyzing symbols.

Because "+" and "++" are both valid symbols, but "++" is not a valid symbol, the expression is resolved to I ++ j.

Another example is a -- B, which indicates a-Negative B. But the compiler resolves it to a -- B, and the final compilation fails. So the code should be written like this: a--B;

 

3. Do you really understand I ++?

The first time I saw this example, I was on a test paper. if you can tell the correct answer to the code snippet below, there is no need to read this part, if not, you may have entered a "misunderstanding ".

            int i = 0;            i = i++;            Console.WriteLine(i); // 1 or 0?

When I remember I ++ and ++ I, I did not remember "add first" or "add later", but directly remembered "computing result ".

Operation Result of I ++: The expression value is equal to I, and the value of I is incremented by 1.

++ I operation result: the expression value is equal to I + 1, and the I value is automatically added 1.

That is to say:

I = I ++; // the result of the expression on the right is equal to I itself, then the value assignment operator is executed, and the result of the final I is I itself.

 

4. What is the value of dividing zero by zero?


Some people may say, "primary school students know that Division 0 cannot be used." Congratulations, if so, correct. However, you can only get 6 points (out of 10 points ). Why? Check the code!

Static void Main (string [] args) {float x = 0; float result = x/x; Console. writeLine (result); // output result: Non-digital Console. read ();}

This topic is just an introduction. For more information, see Single. NaN.

 

The operator of pitfall is indeed very difficult.

 


How do bitwise operators work?

First, the binary and operation rules: 1 & 1 = 1 1 & 0 = 0 0 & 0 = 0
Binary or operation rules: 1 | 1 = 1 1 | 0 = 1 0 | 0 = 0

Further: Binary and decimal conversion:
The power of N cannot be 2, so we have to explain it in an example:
1111 1111 = 128*1 + 64*1 + 32*1 + 16*1 + 8*1 + 4*1 + 2*1 + 1*1

0000 0000 = 128*0 + 64*0 + 32*0 + 16*0 + 8*0 + 4*0 + 2*0 + 1*0

15 = binary: (0000 1111)
127 = binary: (1111 1111)
Bitwise AND nature are (0000 1111) = 15

128 = binary: (0001 0000 0000)
127 = binary: (0000 1111 1111) (0 for high position)
Bitwise OR (0001 1111 1111) = 255

Answer:
Binary "and" operation rule: 1 & 1 = 1 1 & 0 = 0 0 & 0 = 0
Binary "or" operation rule: 1 | 1 = 1 1 | 0 = 1 0 | 0 = 0

Bitwise AND is the bitwise operation from high to low.
So from high to low, 15 & 127:

15 = binary: (0000 1111)
127 = binary: (1111 1111)
There are already too many other
0000 1111
Note: "and" Operation Rules: 1 & 1 = 1 1 & 0 = 0 0 & 0 = 0

Priority of the C Operator

Appendix C Operator priority and associativity
Priority operator meaning calculation type associativity
1 ()
[]
->
, Parentheses
Subscript operator
Point to struct member operators
Structure member operator from left to right
2!
~
++ --
(Type keyword)
+-
*
&
Sizeof logical non-Operator
Bitwise Inverse Operator
Auto-increment and auto-tip Operators
Forced type conversion
Positive and Negative Operators
Pointer Operator
Address Operator
Length operator from right to left
3 */% multiplication, division, and remainder operators: left-to-right
4 +-addition and subtraction operators: left-to-right
5 <
> Left shift operator
Right Shift Operator binocular orientation from left to right
6 <<=>>= less than, greater than, less than or equal to, and greater than or equal to the link from left to right
7 =! = Equal to or not equal to the link from left to right
8 & bitwise AND operator bit operations from left to right
9 ^ bitwise OR operator bit operations from left to right
10 | bitwise OR operator bit operations from left to right
11 & logical and operator bitwise operations from left to right
12 | logical or operator bit operations from left to right
13? : The conditional operators are displayed from the right to the left.
14 = + =-= * =
/=%= <<=>>=<=^= | = The value assignment operator.
15. The comma operation sequence is from left to right.

Related Article

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.