# C# operators

## What is the operator?

The main purpose of the computer is to solve real-life problems by calculating binary numbers. When we think of calculation, we usually think of addition, subtraction, multiplication, division, etc. A computer can do a lot of calculations. We use "+" for addition and "-" for subtraction. Similarly, a computer uses different types of symbols to perform calculations. These symbols are called operators.

## Types of operators

C# uses many types of built-in operators. These are:

**Arithmetic Operators****Assignment Operators****Comparison Operators****Logical Operators****Bitwise Operators****Miscellaneous Operators****Null coalescing operator**

## 1. Arithmetic Operators

Let's assume variable

**X holds 10**and**Y holds 3**. The following table represents Arithmetic Operators.```
int x = 10;
int y = 20;
int sum = x + y; // sum is 30
int difference = x - y; // difference is -10
int product = x * y; // product is 200
int quotient = y / x; // quotient is 2
```

## 2. Assignment Operators

Example:

```
int x = 10; // 1010 in binary
int y = 20; // 10100 in binary
// Basic assignment operator
x = y; // x is now 20
// Compound assignment operators
x += y; // x is now 40
x -= y; // x is now 20
x *= y; // x is now 400
x /= y; // x is now 20
x %= y; // x is now 0
// Bitwise assignment operators
x &= y; // x is now 0 (00000 in binary)
x |= y; // x is now 20 (10100 in binary)
x ^= y; // x is now 0 (00000 in binary)
// Bitwise left shift and right shift assignment operators
x <<= y; // x is now 160 (10100000 in binary)
x >>= y; // x is now 0 (00000 in binary)
```

## 3. Comparison Operators

Let's assume variable

**X holds 10**and**Y also hold 10**. The following table represents Comparison Operators.Example:

```
int x = 10;
int y = 20;
// Equality and inequality operators
bool isEqual = x == y; // isEqual is false
bool isNotEqual = x != y; // isNotEqual is true
// Greater than and less than operators
bool isGreater = x > y; // isGreater is false
bool isLess = x < y; // isLess is true
// Greater than or equal to and less than or equal to operators
bool isGreaterOrEqual = x >= y; // isGreaterOrEqual is false
bool isLessOrEqual = x <= y; // isLessOrEqual is true
```

## 4. Logical Operators

Let's assume variable X holds Boolean "true" and Y holds Boolean "false". The following table represents Logical Operators.

Example:

```
bool x = true;
bool y = false;
// Logical AND operator
bool and = x && y; // and is false
// Logical OR operator
bool or = x || y; // or is true
// Logical NOT operator
bool not = !x; // not is false
```

## 5. Bitwise Operators

Bitwise Operators are a bit difficult. Let's take a look at the symbols, names, and examples of Bitwise Operators and then discuss them in detail. Then it will be very clear. But before that, you need to have an idea about the binary number system.

Before discussing Bitwise Operators, let's remind the truth table of the OR gate, OR gate, & XOR gate.

### AND Gate :

If A and B both are 1 (ON) then the output will be 1 (ON)

### OR Gate :

If either A or B is 1(ON) then the output will be 1 (ON)

### XOR Gate:

Suppose a house is rented out but not to bachelors. Lets Men = 1 and Women = 0. Men & Women can get house rent and output will 1. But (Men & Men) or (Women & Women) can't get house rent because they are a bachelor and their output will be 0.

### Bitwise AND (&) Operator

You have seen 12 & 13 = 12 in the Bitwise Operator table. But how did it happen? Ok, let's calculate.

The binary value of 12 = 1100 and 13 = 1101. If we calculate these binary numbers by following the truth table of AND gate.

Please look, at 12 & 13 = 1100 which is equal to 12.

Example:

```
int x = 12; // 1100 in binary
int y = 13; // 1101 in binary
int result = x & y; // result is 12 (1100 in binary)
// OR
x &= y; // x is now 12 (1100 in binary)
```

### Bitwise OR ( | ) Operator

Similarly, we can calculate 12 | 13 = 13 with the help of the OR gate truth table.

You can see that, 12 | 13 = 1101 which is equal to 13.

Example:

```
int x = 12; // 1100 in binary
int y = 13; // 1101 in binary
int result = x | y; // result is 13 (1101 in binary)
// OR
x |= y; // x is now 13 (1101 in binary)
```

### Bitwise XOR ( ^ ) Operator

We have writeen 12 ^ 25 = 21. Let's calculate.

25 = 11001 ( 5 digits)

12 = 1100 (4 digits)

we can add an extra 0 at the front. Then 12 = 01100 ( 5 digits). Now we can calculate 12 ^ 25 with the help of the XOR gate truth table.

Here, 12 ^ 25 = 10101 which is equal to 21. Because 21 = 10101.

### Bitwise Left Shift ( << ) Operator

Bitwise left shift means shift bit from the right side of the point to the left side. Let's understand with an example.

We can write 12 = 1100.0000 because we know 0 on the right side of the point is valueless. Now If we left shift it by 2 then 12<<2 = 110000.00 which is equal to 48.

For clear understanding,

13 = 1101.0000

13 << 2 = 110100.00 = 52

We can say with the left shift operator, we

**Gain**the bit.### Bitwise Right Shift ( >> ) Operator

It shifts bit or bits from the left side of the point to the right side. Example:

13 = 1101.00

13 >> 2 = 11.0100 = 3

We can say with the right shift operator, We

**Lose**the bit.### Complement ( ~ ) Operator

To understand the complement ( ~ ) operator you must know how to calculate 1's complement and 2's complement.

The complement operator turns the binary digit into the opposite digit.

Example:

~1 = 0

~0 = 1

~11 = 00

~00 = 11

~ 101 = 010

But how ~12 = -13 ?? Let's do some calculations. Here we need 8 digits of the binary value of a number because we want to find 1's complement and 2's complement.

12 = 00001100

~12 = 11110011 ( 1's complement of 12 which also -12 but computer cant store 1's complement)

But why 11110011 is -13? We know computers can't store a negative value. If we want to store -13 in our computer, we should store it as a positive value. let's make -13 first by 1's complement.

13 = 00001101

~13 = 11110010 ( 1's complement )

~13 means -13 but the computer can't store it. But how we can store it? we need to calculate its 2's complement. 2's complement is -13 but in a positive format. Let's find 2's complement of 13. It's not difficult. Just add 1 with the 1's complement.

~13 = 11110010 ( 1's complement )

+1

--------------------

11110011 ( 2's complement of 13 which is -13 but in positive formate )

Wait. 2's complement of 13 = 11110011 which is equal to ~12.

So, we can say ~12 = -13.

## 6. Miscellaneous Operators

```
int x = 10;
double y = 20.5;
// sizeof operator
int sizeOfInt = sizeof(int); // sizeOfInt is 4
// typeof operator
Type typeOfX = typeof(x); // typeOfX is System.Int32
Type typeOfY = typeof(y); // typeOfY is System.Double
// Ternary operator
string message = x > y ? "x is greater than y" : "x is not greater than y"; // message is "x is not greater than y"
// is operator
bool isInt = x is int; // isInt is true
bool isDouble = y is double; // isDouble is true
bool isString = message is string; // isString is true
```

## 7. Null coalescing operator

The null coalescing operator (??) in C# is a shorthand way to check for null before performing a null-coalescing operation. It returns the left operand if it is not null, or the right operand if the left operand is null.

Here is an example that demonstrates the use of the null coalescing operator in C#:

```
int? x = null;
int y = 10;
// Null coalescing operator
int result = x ?? y; // result is 10
```

You can also use the null coalescing operator in an assignment statement, like this:
```
int? x = null;
int y = 10;
// Null coalescing operator
x ??= y; // x is now 10
```

## Post a Comment