Wednesday, May 6, 2020

C# Tutorial: 03 - C# Operators

Tutorial 03: C# Operators

Like all programming languages, C# also have operators. Operators are basically symbols which are used along with operands  as follows:
  • Arithmetic (+, -, *, /, %, ++, --)
  • Relational (<, >, <=, >=, !=, ==)
  • Logical (&&, ||, !)
  • Bit-wise (<<, >>, |, &, ^, ~)
Arithmetic Operators
These are used for performing arithmetic operations. Create a new Console Application and use following example:


After execution you will see following Output on screen:


Relational Operators:
These operators are used for comparing operands which are being used with them. They return either true or false based on their comparison. See following


Logical Operators:
Logical operators allows a program or part of program to make decision. The multiple conditions which are being used, concludes to a decision. Each operand evaluates to either true or false based on its condition. The values of operands are then used to evaluate the complete expression.

Bit-wise Operators:
& (bit-wise AND)
Two numbers are required as operands and AND is performed on each bit of 02 numbers. AND generates 1 if both bits are 1.
| (bit-wise OR)
Two numbers are required as operands and OR is performed on each bit of 02 numbers. OR generates 1 if any of the 2 bits are 1.
^ (bit-wise XOR)
Two numbers are required as operands and XOR is performed on each bit of 02 numbers. XOR generates 1 if 2 bits are different.
 << (bit-wise left shift)
Two numbers are required as operands, then left shifts the bits of the first operand, the second operand decides the number of places to shift.
>> (bit-wise right shift)
Two numbers are required as operands, right shifts the bits of the first operand, the second operand decides the number of places to shift.
 ~ (bit-wise NOT)
It takes a number and all of its bits are inverted.
See the following program and execute it in your IDE by creating a Console Application. Output is mentioned in comments


In next lesson we will discuss conditional statements in C#. If you have any questions feel free to ask in comments.

No comments:

Post a Comment