C# Operators
C# Operators
To perform operations on values of different data types, operators are used. For example, the + operator is used to make the addition of 2 numbers.
For the different data types, let's see the table of operators available
Arithmetic Operators:
There are operators used to carry out numerical mathematical operations. The list of operators available in C # is below.
Operator
Description
+
used to add operands

used to subtract operands
*
used to multiply operands
/
used to divide operands
++ (increment operator)
increases the value by 1
 (decrement operator)
decreases the value by 1
% (modulus operator)
gives the remainder after division
Relational operators
These are operators used for numerical relational operations. The list of relational operators available in C # is given below.
Operator
Description
==
Tests whether or not the values of two operands are identical, then the condition becomes valid if yes.
!=
Tests whether or not the values of two operands are identical, so the condition becomes valid if values are not identical.
>
Checks if the left operand value is greater than the right operand value if the condition becomes valid if yes.
<
Tests whether the left operand value is less than the right operand value, if the condition becomes valid if yes.
>=
Checks if the left operand 's value is greater than or equal to the right operand 's value, if the condition becomes valid if yes.
<=
Tests if the left operand value is less than or equal to the right operand value, and if yes, the condition becomes valid.
Logical operators
These are operators used for performing valuesbased logical operations. The list of operators available in C # is below.
Operator
Description
&&
This is the operator for Logical AND. If the operands are both true, then the situation becomes true.

This is the operator for Logical OR. If one of the operands is real, then the situation becomes real.
!
This is the operator of Logical NOT.
Bitwise operator
In this, we will learn in depth about the operators of bitwise and bit shift in C #. 4 bit operators and 2 bit shift operators are given by C #.
To perform bit level operations on integer (int, long, etc) and boolean data, bitwise and bit shift operators are used. In real life cases, these operators are not widely used.
List of bitwise operators
Operator
Operator name
~
Bitwise Complement
&
Bitwise AND

Bitwise OR
^
Bitwise Exclusive OR (XOR)
<<
Bitwise left shift
>>
Bitwise right shift
1.Bitwise OR
The operator of Bitwise OR is defined by””. On the corresponding bits of two operands, it performs bitwise OR operations. If any of the bits is 1, then the outcome is 1. Otherwise the answer is 0.
If the operands are booltype, the bitwise OR operation between them is equal to the logical OR operation.
Example
1400001110
1100001011
After bitwise OR
1500001111
2.Bitwise AND
The operator of Bitwise AND is defined as &. It performs the operation bitwise AND on the corresponding bits of two operands. If either of the bits is 0, the outcome is 0. Otherwise, 1. is the result.
If the operands are of type bool, the operation of bitwise AND is identical to the operation of logical AND between them.
1400001110
1100001011
After bitwise AND
1000001010
3. XOR bitwise
The operator of Bitwise XOR is defined by ^. The bitwise XOR operation is performed on the corresponding bits of two operands. The result is 0.0, if the corresponding bits are the same. The result is 1. if the corresponding bits are different.
If the operands are booltype, the bitwise operation of XOR is equal to the logical operation of XOR between them.
1400001110
1100001011
After bitwise XOR
500000101
4. Complement Bitwise
The operator of Bitwise Complement is defined by ~. It is a single operator, i.e. it only works with one operand. What bit is reversed by the ~ operator, i.e. 1 changes to 0 and 0 to 1.
For instance,
2600011010
After Bitwise Complement operation on 26:
~00011010 =11100101=226
5. bitwise left shift
The operator of the bitwise left shift is defined by < <. The operator < < moves a number to the left with the number of bits defined. To the least important bits, zeroes are applied.
In decimal terms, this is equivalent to
num*2bits
42=101010
After bitwise left shift
42<<1=84 (in binary 1010100)
6. bitwise right shift
The operator of the bitwise right shift is defined by > >. The > > operator pushes a number to the right with the number of bits defined. The first operand moves the number of bits defined by the second operand to the right.
In decimal terms, this is equivalent to
Floor(num/2bits)
42=101010
After bitwise right shift
42>>1=21 (in binary 010101)
Assignment Operator
=
Simple assignment operator. Assigns values from right side operands to left side operand
C = A + B will assign the value of A + B to C
+=
Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.
C += A is equivalent to C = C + A
=
Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.
C = A is equivalent to C = C  A
*=
Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.
C *= A is equivalent to C = C * A
/=
Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.
C /= A is equivalent to C = C / A
%=
Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.
C %= A is equivalent to C = C % A
<<=
Left shift AND assignment operator.
C <<= 2 is same as C = C << 2
>>=
Right shift AND assignment operator.
C >>= 2 is same as C = C >> 2
&=
Bitwise AND assignment operator.
C &= 2 is same as C = C & 2
^=
Bitwise exclusive OR and assignment operator.
C ^= 2 is same as C = C ^ 2
=
Bitwise inclusive OR and assignment operator.
C = 2 is same as C = C  2
Operator precedence
The precedence of the operator determines the grouping of words in an expression. This impacts an expression 's assessment. Some operators have greater precedence than others; the multiplication operator, for instance, has greater precedence than the addition operator.
Postfix
() [] > . ++  
Left to right
Unary
+  ! ~ ++   (type)* & size of
Right to left
Multiplicative
* / %
Left to right
Additive
+ 
Left to right
Shift
<< >>
Left to right
Relational
< <= > >=
Left to right
Equality
== !=
Left to right
Bitwise AND
&
Left to right
Bitwise XOR
^
Left to right
Bitwise OR

Left to right
Logical AND
&&
Left to right
Logical OR

Left to right
Conditional
?:
Right to left
Assignment
= += = *= /= %=>>= <<= &= ^= =
Right to left
Comma
,
Left to right