Operators
In this session we are going to discuss about various types of operators in use in C#
- Arithmetic Operator
- Relational Operator
- Logical Operator
- Bitwise Operator
- Assignment Operator
- Misc Operator
- Conditional Operator
Arithmetic Operator
Relational Operator
Logical Operator
&& || !
Use Bitwise Operator
In case of bitwise operator we need to discuss about what operations these symbols perform.
suppose we have 2 variables A and B.
if A = 9 and B = 12
then A in binary is 1001, B in binary is 1100
Then A | B = A OR B = 00001001 OR 00001100 =0 0 0 0 1 1 0 1 = 13
Then A & B = A AND B = 00001001 AND 00001100 = 0 0 0 0 1 0 0 0 = 8
Then A^B = A XOR B = 00001001 XOR 00001100 = 0 0 0 0 0 1 0 1 = 5
Then A<<2 = Shift of A bitwise by 2 digits on leftside = (00001001)<< 2 = (00100100) = 36
Then A>>2 = Shift of A bitwise by 2 digits on rightside = 2>>(00001001) = (00000010) = 2
Now lets test these examples in C#
Assignment Operator
= += -= *= /= %= <<= >>= &= ^= |=
Use Assignment Operator
Miscillaneous Operator
sizeof(), typeof(), *, &, is, as
In case of bitwise operator we need to discuss about what operations these symbols perform.
suppose we have 2 variables A and B.
if A = 9 and B = 12
then A in binary is 1001, B in binary is 1100
Then A | B = A OR B = 00001001 OR 00001100 =0 0 0 0 1 1 0 1 = 13
Then A & B = A AND B = 00001001 AND 00001100 = 0 0 0 0 1 0 0 0 = 8
Then A^B = A XOR B = 00001001 XOR 00001100 = 0 0 0 0 0 1 0 1 = 5
Then A<<2 = Shift of A bitwise by 2 digits on leftside = (00001001)<< 2 = (00100100) = 36
Then A>>2 = Shift of A bitwise by 2 digits on rightside = 2>>(00001001) = (00000010) = 2
Now lets test these examples in C#
Assignment Operator
= += -= *= /= %= <<= >>= &= ^= |=
Use Assignment Operator
Miscillaneous Operator
sizeof(), typeof(), *, &, is, as
Conditional Operator
? :
Use Conditional Operator
In the below example the term A>B ? 0:1; needs to be explained
Here it means
if(A>B)
val = 0;
else
val=1;
Comments
Post a Comment