Example
The example provided below shows how to use Assignment Operators within a program.
[csharp]
using System;
namespace SPLessons
{
class Assignment
{
public static void main(String[] args)
{
int a=10,b;
b=a; //Assignment of Operands
Console.Write( (“Value:”+b);
b+=a; //Add value and then Assign value to Operand
Console.Write( (“Value:”+b);
b-=a; // Subtract value and then Assign value to Operand
Console.Write( (“Value:”+b);
b*=a; // Multiply value and then Assign value to Operand
Console.Write( (“Value:”+b);
b/=a; // Divide value and then Assign value to Operand
Console.Write(“Value:”+b);
}
}
}
[/csharp]
Relational/Comparison Operators
Description
Using these operators, values of two operands can be compared. Boolean values(True or False) are returned after the comparison.
Operators
Operator |
Description |
== |
Checks equal or not, if yes then returns true |
!= |
Checks Equal or not, if values are not equal then condition becomes true |
> |
Checks for greater value |
< |
Checks for lower value |
>= |
Checks for greater than or equal to the value |
<= |
Checks for less than or equal to the value |
Example
The example provided below shows how to use Relational/Comparison Operators within a program.
[csharp]
using System;
namespace SPLessons
{
class Comparison
{
public static void main(String[] args)
{
if(a>b)
{
Console.WriteLine("a is greater than b");
}
if(a<b)
{
Console.WriteLine("a is less than b");
}
if(a>=b)
{
Console.WriteLine("a is greater than,equal to b");
}
if(a<=b)
{
Console.WriteLine("a is less than,equal to b");
}
if(a==b)
{
Console.WriteLine("a is equal to b");
}
if(a!=b)
{
Console.WriteLine("a is not equals to b");
}
}
}
}
}
[/csharp]
Description
Below mentioned are some of the bitwise operators supported by C#.
Operators
Operator |
Symbol |
Form |
Operation |
Right shift |
>> |
x>>y |
all bits in x shifted right y bits |
Left shift |
<< |
x<<y |
all bits in x shifted left y bits |
Bitwise AND |
& |
x&y |
each bit in x AND each bit in y |
Bitwise OR |
| |
x|y |
each bit in x OR each bit in y |
Bitwise XOR |
^ |
x^y |
each bit in x XOR each bit in y |
Bitwise NOT |
~ |
~x |
all bits in x flipped |
Example
The example provided below shows how to use Bitwise Operators within a program.
[csharp]
using System;
namespace SPLessons
{
Class Bitwise
{
static void main(string[] args)
{
int w=2, x=10,y=20,z; //binary equivalent of 10 =01010 and 20 =10100
z=x|y;
Console.Write("\nBinary OR operator:"+z);
z=x&y;
Console.Write("\nBinary AND operator:"+z);
z=~x;
Console.Write("\nBinary Ones Complement:"+z);
z=x^y;
Console.Write("\nBinary XOR operator:"+z);
z=x<<w;
Console.Write("\nBinary Left Shift operator:"+z);
z=x>>w;
Console.Write("\nBinary Right Shift operator:"+z);
}
}
}
[/csharp]
Description
Using these operators the operations are performed on operands which are either true or false. Let us consider x=true, y=false.
Operators
Operator |
Name of Operator |
Output |
&& |
AND Operator |
Output is 1 only when conditions on both sides of operator becomes true |
|| |
OR Opeator |
Output is 0 only when conditions on both sides of operator becomes false |
! |
NOT Operator |
Gives inverted output |
Example
The example provided below shows how to use Logical Operators within a program.
[csharp]
using System;
using System.Text;
namespace SPLessons
{
class Program
{
static void Main(string[] args)
{
bool x=true,y=false;
if(x||y)
{
Console.Write("\nTrue");
}
if(x&&y)
{
Console.Write("\nfalse");
}
if(!( x&&y ))
{
Console.Write("\ntrue");
}
}
}
}
[/csharp]