C# operators
C#Operator
An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. C# has a rich set of built-in operators, which are classified as follows:
Arithmetic operators
Relational operators
-
Logical operators
Bitwise operators
Assignment operators
Miscellaneous operations Operators
This tutorial will explain arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators and other operators one by one.
Arithmetic operators
The following table shows all the arithmetic operators supported by C#. Assume that the value of variable A is 10 and the value of variable B is 20, then:
Description | Example | |||
---|---|---|---|---|
Add the two operands | A + B will get 30 | |||
Subtracting the second operand from the first operand | A - B will give -10 | |||
Multiply the two operands | A * B will get 200 | |||
numerator Dividing by the denominator | B / A will get 2 | |||
modulo operator, the remainder after integer division | B % A will get 0 | |||
auto-increment operator, the integer value is increased by 1 | A++ will get 11 | |||
Decrement operator, reduce the integer value by 1 | A-- will get 9 |
Description | Example | |||
---|---|---|---|---|
Checks whether the values of the two operands are equal. If they are equal, the condition is true. | (A == B) is not true. | |||
Checks whether the values of the two operands are equal. If they are not equal, the condition is true. | (A != B) is true. | |||
Checks whether the value of the left operand is greater than the value of the right operand, if so, the condition is true. | (A > B) is not true. | |||
Checks whether the value of the left operand is less than the value of the right operand, if so the condition is true. | (A < B) is true. | |||
Checks whether the value of the left operand is greater than or equal to the value of the right operand, if so the condition is true. | (A >= B) is not true. | |||
Checks whether the value of the left operand is less than or equal to the value of the right operand, if so the condition is true. | (A <= B) is true. |
Description | Instance | |
---|---|---|
is called the logical AND operator. The condition is true if both operands are non-zero. | (A && B) is false. | |
is called the logical OR operator. The condition is true if either of the two operands is non-zero. | (A || B) is true. | |
is called the logical NOT operator. Used to reverse the logical state of the operand. The logical NOT operator will make the condition false if it is true. | !(A && B) is true. |
using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { bool a = true; bool b = true; if (a && b) { Console.WriteLine("Line 1 - 条件为真"); } if (a || b) { Console.WriteLine("Line 2 - 条件为真"); } /* 改变 a 和 b 的值 */ a = false; b = true; if (a && b) { Console.WriteLine("Line 3 - 条件为真"); } else { Console.WriteLine("Line 3 - 条件不为真"); } if (!(a && b)) { Console.WriteLine("Line 4 - 条件为真"); } Console.ReadLine(); } } }When the above code is When compiled and executed, it produces the following results:
Line 1 - 条件为真 Line 2 - 条件为真 Line 3 - 条件不为真 Line 4 - 条件为真Bitwise OperatorsBitwise operators act on bits and perform operations bit by bit. The truth table for &, | and ^ is as follows:
q | p & q | p | q | p ^ q | |
---|---|---|---|---|
0 | 0 | 0 | 0 | |
1 | 0 | 1 | 1 | |
1 | 1 | 1 | 0 | |
0 | 0 | 1 | 1 |
Description | Example | |
---|---|---|
The binary AND operator copies one bit to the result if both operands are present at the same time. | (A & B) will get 12, which is 0000 1100 | |
If present in any operand, binary OR operation character is copied into the result. | (A | B) will get 61, which is 0011 1101 | |
if present in one of the operands but not both The binary XOR operator copies one of the two operands into the result. | (A ^ B) will get 49, which is 0011 0001 | |
The two's complement operator is a unary operator with " Flip" bit effect. | (~A ) will get -61, which is 1100 0011, 2's complement, signed binary number. | |
Binary left shift operator. The value of the left operand is shifted left by the number of bits specified by the right operand. | A << 2 will get 240, which is 1111 0000 | |
Binary right shift operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand. | A >> 2 will get 15, which is 0000 1111 |
Operators | Description | Example |
---|---|---|
= | Simple assignment operator, assigns the value of the right operand to the left operand | C = A + B will assign the value of A + B to the C |
+= | addition and assignment operator, adding the right operand to the left operand The result is assigned to the left operand | C += A, which is equivalent to C = C + A |
-= | subtraction and assignment operator. The result of subtracting the right operand from the operand is assigned to the left operand | C -= A which is equivalent to C = C - A |
*= | The multiplication and assignment operator assigns the result of multiplying the right operand by the left operand to the left operand | C *= A which is equivalent to C = C * A |
/= | The division and assignment operator assigns the result of dividing the left operand by the right operand to the left operand | C /= A is equivalent to C = C / A |
%= | Modulo and assignment operator, find the modulus of two operands and assign the value to the left operand | C %= A is equivalent to C = C % A |
left shift and assignment operator | C <<= 2 is equivalent to C = C < < 2 | |
Right shift and assignment operator | C >>= 2 is equivalent to C = C >> 2 | |
Bitwise AND and assignment operator | C &= 2 is equivalent to C = C & 2 | |
Bitwise XOR and assignment operator | C ^= 2 is equivalent to C = C ^ 2 | |
Bitwise OR and assignment operator | C |= 2 is equivalent to C = C | 2 |
Category | Operator | Associativity |
---|---|---|
Suffix | () [] -> . ++ - - | From left to right |
One yuan | + - ! ~ ++ - - (type) * & sizeof | From right to left |
Multiplication and division | * / % | From left to right |
Addition and subtraction | + - | From left to right |
Shift | << ; >> | From left to right |
Relationship | < <= > >= | From left to right |
Equal | == != | From left to right |
bits AND | & | from left to right |
bit XOR XOR | ^ | from left To the right |
| | From left to right | |
&& | From left to right | |
|| | From left to right | |
?: | From right to left | |
= += -= *= /= %=>>= <<= &= ^= |= | right to left | |
, | From left to right |
using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { int a = 20; int b = 10; int c = 15; int d = 5; int e; e = (a + b) * c / d; // ( 30 * 15 ) / 5 Console.WriteLine("(a + b) * c / d 的值是 {0}", e); e = ((a + b) * c) / d; // (30 * 15 ) / 5 Console.WriteLine("((a + b) * c) / d 的值是 {0}", e); e = (a + b) * (c / d); // (30) * (15/5) Console.WriteLine("(a + b) * (c / d) 的值是 {0}", e); e = a + (b * c) / d; // 20 + (150/5) Console.WriteLine("a + (b * c) / d 的值是 {0}", e); Console.ReadLine(); } } }
When the above code is compiled and executed, it will produce the following results:
(a + b) * c / d 的值是 90 ((a + b) * c) / d 的值是 90 (a + b) * (c / d) 的值是 90 a + (b * c) / d 的值是 50