Speaking of C # operators and expressions, the small partners must think very simple, in fact, to use a good expression, is not an easy thing. A good expression allows you to do more with less, such as ternary expressions, which allows you to write fewer than n if and case statements.
An expression consists of an operand (operand) and an operator (operator)
1. Operator classification and precedence
2. Operators are categorized by number of operations
Unary operators |
-X x + + x-- |
Binary operators |
X+y x+1 |
Ternary operators |
C? X:y |
3. Classification of expressions
By number of operators can be divided into: unary expression, two-tuple expression, ternary expression
Categorized by Purpose: Regular expressions, lambda expressions, and so on.
Key points:
1./ Division operator 5/2=2 (not rounding, but rounding off decimals)
2.% modulo operator 5%2=1 5.0%2.2=0.6 (2nd integer divisible by 1th number remainder)
3. + + increment operator --decrement operator
The above two operators can be before or after, but before and after, their order of execution is different for example:
int a=1; int b=a++;
After the operator is in, the program assigns the value of A to B, and then executes +1 on A;
The result of this operation is: a=2 b=1
If you write the following example:
int M=1;int n=++m;
The result after execution is: m=2 n=2;
is actually equivalent to an int m=1; m=m+1; int n=m;
4. Shift operator << (shift left) and >> (shift right)
These two operators are less commonly used and are slightly more complicated by official understanding.
Can understand this: in fact, the data is converted into binary left and right movement;
Shift right to left 0, left to right 0, after the extra part is removed.
such as: Put 89 right displacement one, you can calculate:
String flag = Convert.ToString (89, 2); This is converting your 89 to 2 binary number.
Flag results: 1011001
You need to shift right, 0 on the left, one more out of the back.
Int J = Convert.ToInt32 ("0101100", 2); The 2 binary is converted to 10 binary number.
Results: 44
Displacement is so simple.
5. Logical XOR Operator ^
For simple logical operators & | | We are familiar, but for different or many people do not understand, and even rarely use
Understand this operator, first of all it is a logical operator, but it is also an integer that can be calculated
The first Boolean operation, simply said, is that two operands are different, true;
such as: true ^ true=false; False^false=false; True ^false=true; False ^true=true;
When manipulating integers, bitwise operations are performed, such as:
100^45=73
Examples of numerical operations
3 Features of bitwise XOR:
(1) 0^0=0,0^1=1 0 xor or any number = any number
(2) 1^0=1,1^1=0 1 XOR or any number--any number taken against
(3) 1^1=0,0^0=0 any number of different or self = put yourself in 0
Example: 10100001^00010001=10110000
Several common uses of bitwise XOR:
(1) Turn some specific bits upside down
For example, the 2nd and 3rd bits of the logarithm 10100001 are flipped, and the number can be bitwise XOR or operated with 00000110.
0100001^00000110 = 10100111
(2) The interchange of two values is implemented without the use of temporary variables.
For example, a value of two integer a=10100001,b=00000110 can be exchanged by the following statement:
A = A^b;//a=10100111
b = b^a;//b=10100001
A = A^b;//a=00000110
(3) In assembly language is often used to set the variable 0:
XOR A,a
(4) quickly determine whether two values are equal
Example 1: To determine whether the two integers a and b are equal, it can be implemented by the following statements:
Return ((a ^ b) = = 0)
- Character and string operations
Since a character can be implicitly converted to an integral type, such as ' a ' +6=103 A is converted to Unicode 97
' A ' * ' b ' = 9506 equals 97*98
' A ' > ' B ' result is false equivalent to 97>98 return false
' A ' + ' Good morning ' return ' a Good morning '
Thus. Different types of operations perform an implicit conversion in C # first
Operators and expressions in C #