# C++ Operators .

In this session i have discussed C++ Operators .

Operators is a symbol that perform some action on the values of variable like + operator is used to perform the addition action .

C++ supports following types of operators .

1-Arithmetic Operators .

2-Relational Operators .

3-Logical Operators .

4-Assignment Operators .

5-Increment and Decrement Operators .

6-Conditional Operators .

7-Bitwise Operators .

8-Special Operators .

Arithmetic Operators :

The C++ Arithmetic operators perform all the mathmetical and logical operation like addition , subtraction , multiplication , division etc .

 Operator Meaning Description x=20 y=10 + Plus or unary plus Add the two numbers x+y //30 - Substraction or unary minus Subtract the first number from second number x-y //10 * Multiplication . Multiplies the two numbers x*y //200 / Division. Divide the two numbers x/y // 2 % Modulo division . Return the modulus x%y // 0 .

Example : Program that will add the two numbers .

``` #include<iostream> using namespace std; int main() { int x, y,sum; x=20; y=10; sum=0; sum=x+y; cout<<"Sum : "<<sum; return 0; } ```
```    OUTPUT : Sum : 30
```

Example : Program that will subtract the two numbers .

``` #include<iostream> using namespace std; int main() { int x, y,sub; x=20; y=10; sub=0; sub=x-y; cout<<"Sub : "<<sub; return 0; } ```
```    OUTPUT : Sub : 10
```

Example : Program that will multiplies the two numbers .

``` #include<iostream> using namespace std; int main() { int x, y,mul; x=20; y=10; mul=0; mul=x*y; cout<<"Mul : "<<mul; return 0; } ```
```    OUTPUT : Mul : 200
```

Example : Program that will divide the two numbers .

``` #include<iostream> using namespace std; int main() { int x, y,div; x=20; y=10; div=0; div=x/y; cout<<"Div : "<<div; return 0; } ```
```    OUTPUT : Div : 2
```

Example : Program that will returns the modulus .

``` #include<iostream> using namespace std; int main() { int x, y,mod; x=21; y=10; mod=0; mod=x%y; cout<<"Mod : "<<mod; return 0; } ```

OUTPUT : Mod : 1

Relational Operators :

Relational operators is used to compare two quantities to take certain decision . For example you may compare the price of two items .

 Operator Meaning Description x=20 y=10 < is less than Check if the left side operand less to right side operand then condition become true x < y // true <= is less than or equal to Check if the left operand value is less than or equal to right side operand value then condition become true x<=y // true > is greater than Check if left side operand value is greater than right side operand value then condition becomes true x > y // false >= is greater than or equal to Check if left side operand value is greater then or equal to right side operand value then condition become true x>=y //false == is equal to Check if the left side operand value is same to right side of operand value then condition become true x==y // false != is not equal to Check if the left side operand value is not equal to right side of operand value then condition become true x!=y //true .

Example : Program that will check the x value is greater than y value .

``` #include<iostream> using namespace std; int main() { int x, y; x=20; y=10; if(x>y) { cout<<"x is greater than y"; } return 0; } ```
```    x is greater than y
```

Example : Program that will check the condition x value is greater then or equal to y value.

``` #include<iostream> using namespace std; int main() { int x, y; x=10; y=10; if(x >= y) { cout<<"x is greater than or equal to y"; } return 0; } ```
```    OUTPUT : x is greater than or equal to y
```

Example : Program that will check the condition x value is less then y value .

``` #include<iostream> using namespace std; int main() { int x=10; int y=20; if(x < y) { cout<<"x is less than y"; } return 0; } ```
```    OUTPUT x is less than y
```

Example : Program that will check the condtion x value is less then or equal to y value .

``` #include<iostream> using namespace std; int main() { int x=10; int y=11; if(x <= y) { cout<<"x is less than or equal to y"; } return 0; } ```
```    OUTPUT : x is less than or equal to y
```

Example : Program that will check the condition x value is equal to y value .

``` #include<iostream> using namespace std; int main() { int x=10; int y=10; if(x == y) { cout<<"x and y values are same "; } return 0; } ```
```    OUTPUT : x and y values are same
```

Example : Program that will check condtion x is not equal to y value .

``` #include<iostream> using namespace std; int main() { int x=10; int y=20; if(x != y) { cout<<"x and y values are not same "; } return 0; } ```
```    OUTPUT : x and y values are not same
```

Logical Operators :

Logical operators are used to evaluate the two or more conditions . It is combine the relational expression . You can use the any kind of expression . If condition is true then it will return 1 other wise 0 .

 Operator Meaning Description x=10 y=20 && Logical AND It return true if both operads are non zero (x==10 && y==20) //true || Logical OR It returns true if any operand is non zero (x==10 || y==10) //true ! Logical NOT It reverse the condition if operads is true then it become false !(x>y) //true .

Example : Program that will check the condition if both condition is true then it will execute the statement inside the if .

``` #include<iostream> using namespace std; int main() { int x = 10, y = 20; if((x==10) && (y==20)) { cout<<"Both condition true"; } return 0; } ```
```    OUTPUT : Both condition true
```

Example : Program that will check the condition if any one condition is true then the statement inside the if will execute .

``` #include<iostream> using namespace std; int main() { int x = 10, y = 20; if ((x == 10) || (y == 30)) { cout<<" Single Condition true"; } return 0; } ```
```    OUTPUT : Single Condition true
```

Example : Program that will check the condition if condtion is false then the statement inside the if will execute .

``` #include<iostream> using namespace std; int main() { int x = 10, y = 20; if (!(x>y)) { cout<<"Welcome"; } return 0; } ```
```    OUTPUT : Welcome
```

Assignment Operators :

Assignment operators are used to assign the value to variable in C# program . The usual assignment operator is = operator . C# has some short hand assignment operators as given below .

 Statement with assignment operator Statement with shorthand operator x=x+1 x+=1 x=x-1 x-=1 x=x*x x*=x x=x/(n+9) x/=n+9 x=x%b x%=b .

Example :

``` #include<iostream> using namespace std; int main() { int x = 20; x = x + 1; cout<<x; return 0; } ```
```    OUTPUT : 21
```

Example :

``` #include<iostream> using namespace std; int main() { int x = 20; x = x - 1; cout<<x; return 0; } ```
```    OUTPUT : 19
```

Example :

``` #include<iostream> using namespace std; int main() { int x = 20; x = x * x; cout<<x; return 0; } ```
```    OUTPUT : 400
```

Example :

``` #include<iostream> using namespace std; int main() { int x = 20 , n = 1; x = x / (n+9); cout<<x; return 0; } ```
```    OUTPUT : 2
```

Example :

``` #include<iostream> using namespace std; int main() { int x = 20, b=3; x = x % b; cout<<x; return 0; } ```
```    OUTPUT : 2
```

Increment & Decrement Operator :

 Operator Meaning Description x=10 y=20 ++ Increment Its increases the one value of operand x++ // 11 -- Decrement Its decreases the one value of operand y-- // 19 .

Pre-increment ++ operator

It first increases the one value and assign the increases value to variable in given expression .

Example :

``` #include<iostream> using namespace std; int main() { int x, y; x = 20; y = ++x; cout<<"\nx= "<<x; cout<<"\ny= "<<y; return 0; } ```
```    OUTPUT : x= 21
y= 21
```

Post-increment ++ operator

It first assigne the value to variable in given expression and then increase the one value .

Example :

``` #include<iostream> using namespace std; int main() { int x, y; x = 20; y = x++; cout<<"\nx= "<<x; cout<<"\ny= "<<y; return 0; } ```
```    OUTPUT : x= 21
y= 20
```

Pre-decrement -- operator

It first decreases the one value and assign the decreases value to variable in given expression .

Example :

``` #include<iostream> using namespace std; int main() { int x, y; x = 20; y = --x; cout<<"\nx= "<<x; cout<<"\ny= "<<y; return 0; } ```
```    OUTPUT : x= 19
y= 19
```

Post-decrement -- operator

It first assigne the value to variable in given expression and then decrease the one value .

Example :

``` #include<iostream> using namespace std; int main() { int x, y; x = 20; y = x--; cout<<"\nx= "<<x; cout<<"\ny= "<<y; return 0; } ```
```    OUTPUT : x= 19
y= 20
```

Conditional Operator :

Conditional Operator also known as ternary operator(?:) . It is look like below .

``` expression1 ? expression2 :expression3 ```

It uses the three operands . The first operand is used to give the condition and if the condition is true then second operand will be evaluate other wise third operand will be evaluate .

Example : Write a program that will check the maximum value among two values .

``` #include<iostream> using namespace std; int main() { int x=35; int y=20; int max=(x>y)?x:y ; cout<<"MAX= "<<max; return 0; } ```
```    OUTPUT : MAX : 35
```

Bitwise Operator :

Before read the bitwise operator please try to understand the following table .

 X Y X&Y X|Y X^Y ~X 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0 .

 Operator Meaning Description x=12 y=25 & Binary AND operator It is used to add bit of first operand with bit of second operand if both are 1 it will be one other wise 0. x&y // 8 | Binary OR operator It is used to add bit of first operand with bit of second operand if at least one corresponding bit of two operands is 1 it will be 1 other wise 0 . x|y //29 ^ Binary XOR operator It is 1 if other two corresponding operands are opposite Please read the first table . x^y // 21 ~ Binary Ones complement operator It is change the 1 to 0 and 0 to 1 please read the first table . ~x//-13 << Left shift operator It is used to shifts all bits towards left by specified bits x<<1 //24 >> Right shift operator It is used to shifts all bits towards right by specified bits x>>1 //6 .

Bitwise AND(&) Operator :

Lets take a simple example x=12 , y=25 . First we change the values in binary format .

``` 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) & --------------------- 00001000 = 8 (In decimal) ``` ``` #include<iostream> using namespace std; int main() { int x=12; int y=25; cout<<x&y; return 0; } ```
```    OUTPUT : 8
```

Bitwise OR(|) Operator :

Lets take a simple example x=12 , y=25 . First we change the values in binary format .

``` 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) | --------------------- 00011101 = 29 (In decimal) ``` ``` #include<iostream> using namespace std; int main() { int x=12; int y=25; cout<<x|y; return 0; } ```
```    OUTPUT : 29
```

Bitwise XOR(^) Operator :

Lets take a simple example x=12 , y=25 . First we change the values in binary format .

``` 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) ^ --------------------- 00010101 = 21 (In decimal) ``` ``` #include<iostream> using namespace std; int main() { int x=12; int y=25; cout<<(x^y); return 0; } ```
```    OUTPUT : 21
```

Bitwise Complement(~) Operator :

So let 's take a look at how we get ~12 = -13: .

Convert into binary format : 00001100 Simply flip all the bits and we get: 11110011 Well, what's -13 look like in two's complement? Start with positive 13: 00001101, flip all the bits to 11110010, and add 1 to become negative value (-13), 11110011. So if you simply invert the bits in 12, you get the two's complement representation of -13.

``` #include<iostream> using namespace std; int main() { int x=12; cout<<~x; return 0; } ```
```    OUTPUT : -13
```

Bitwise RightShift(<<) Operator :

Lets take a simple example x=12. First we change the values in binary format .

``` 12 = 00001100 (In Binary) --------------------- 12<<1 = 00011000 (right shift by one bit) ``` ``` #include<iostream> using namespace std; int main() { int x=12; cout<<(x<<1); return 0; } ```
```    OUTPUT : 24
```

Bitwise LeftShift(>>) Operator :

Lets take a simple example x=12. First we change the values in binary format .

``` 12 = 00001100 (In Binary) --------------------- 12>>1 = 00000110 (right shift by one bit) ``` ``` #include<iostream> using namespace std; int main() { int x=12; cout<<x>>1; return 0; } ```
```    OUTPUT : 6
```

8-Special Operators

 Operator Meaning Description x=12 y=25 sizeof sizeof operator It is used to get the size of data types or variable . sizeof(x) // 4 exp1?exp2:exp3 Conditional operator It is check the condition just like if else . x>y?x:y //25 , Comma operator It is used to seperate the related expression . x,y . -> dot and arrow operator . It is used to refer the individual member of class , struct , union . obj.x//12 & address operator It is used to get the address of variable . &x * Pointer operator It is used create the pointer . int*x .

Example : Write a program that demonstrate the sizeof operator .

``` #include<iostream> using namespace std; int main() { int a; cout<<sizeof(a); return 0; } ```
```    OUTPUT : 4
```

Example : Write a program that demonstrate the conditional operator .

``` #include<iostream> using namespace std; int main() { int x=20,y=10; int result=x>y?x:y; cout<<result; return 0; } ```
```    OUTPUT : 20
```

Example : Write a program that demonstrate the comma operator .

``` #include<iostream> using namespace std; int main() { int x=20,y=10;//use of comma operator cout<<x; return 0; } ```
```    OUTPUT : 20
```

Example : Write a program that demonstrate the dot operator .

``` #include<iostream> using namespace std; class ABC { public : int x; }; int main() { ABC obj; obj.x=12; cout<<obj.x; return 0; } ```
```    OUTPUT : 12
```

Example : Write a program that demonstrate the address(&) operator .

``` #include<iostream> using namespace std; int main() { int x; cout<<&x; return 0; } ```
```    OUTPUT : 0x28fcfc
```

Example : Write a program that demonstrate the pointer(*) operator .

``` #include<iostream> using namespace std; int main() { int *x,y=10; x=&y; cout<<*x; return 0; } ```
```    OUTPUT : 10
```

# Welcome to AndroidATPoint.

`  `