Table of Contents

In this lesson, you will learn about different types of **C++ operators **and their usage, such as the addition `(+)`

, subtraction `(-)`

, multiplication `(*)`

, and division `(/)`

.

## What is an operator?

An operator is a symbol that performs an operation, such as the addition symbol `+`

, which tells the compiler to add two or more numbers.

**More like this:**

## Type of operators

Below are the type of operators in C++

- Assignment Operators
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Misc Operators

## Assignment Operators

The Assignment Operator is used to assigning a value to a variable.

x = 5;

where the ‘=’ tells the compiler to set the value of x to 5.

Below is the list of assignment operators in programming.

Operator | Description | Example |
---|---|---|

= | To assigns values from right side operands to left side operand | Z = X + Y |

+= | To adds the right operand to the left operand and assign the result to the left operand. | Z += Y |

-= | To subtracts the right operand from the left operand and assigns the result to the left operand. | Z -= Y |

*= | To subtracts the right operand from the left operand and assigns the result to the left operand. | Z * =Y |

/= | To multiplies the right operand with the left operand and assigns the result to the left operand. | Z / =Y |

%= | To takes modulus using two operands and assigns the result to the left operand. | Z % = Y |

<<= | Left shift AND assignment operator. | Z<<=Y |

>>= | Right shift AND assignment operator. | Z>>=Y |

&= | Bitwise AND assignment operator. | Z &= 2 |

^= | Bitwise exclusive OR and assignment operator. | Z ^= 2 |

|= | Bitwise inclusive OR and assignment operator. | Z |= 2 |

## Example of Assignment Operators in C++

#include <iostream> using namespace std; int main () { int x = 10; int output; output = x; cout << " '=' Operator example. The value of output is " << output << endl; output += x; cout << " '+=' Operator example. The value of output is " << output << endl; output -= x; cout << " '-=' Operator example. The value of output is " << output << endl; output *= x; cout << " '-=' Operator example. The value of output is " << output << endl; output /= x; cout << " '/=' Operator example. The value of output is " << output << endl; output %= x; cout << " '%=' Operator example. The value of output is " << output << endl; output = 80; output %= x; cout << " ' %=' Operator example. The value of output is " << output << endl; output <<= 2; cout << " '<<=' Operator example. The value of output is " << output << endl; output >>= 2; cout << " '>>=' Operator example. The value of output is " << output << endl; output &= 2; cout << " '&=' Operator example. The value of output is " << output << endl; output ^= 2; cout << " '^=' Operator example. The value of output is " << output << endl; output |= 2; cout << " '|=' Operator example. The value of output is " << output << endl; return 0; }

‘=’ Operator example. The value of output is 10

‘+=’ Operator example. The value of output is 20

‘-=’ Operator example. The value of output is 10

‘-=’ Operator example. The value of output is 100

‘/=’ Operator example. The value of output is 10

‘%=’ Operator example. The value of output is 0

‘%=’ Operator example. The value of output is 0

‘<<=’ Operator example. The value of output is 0

‘>>=’ Operator example. The value of output is 0

‘&=’ Operator example. The value of output is 0

‘^=’ Operator example. The value of output is 2

‘|=’ Operator example. The value of output is 2

## Arithmetic Operators

Arithmetic, of Mathematical Operators, are used to perform arithmetic operations.

z = x + y

Below are the arithmetic operators available to you.

Operator | Description | Example |
---|---|---|

+ | To adds two operands. | X + Y |

- | To subtracts second operand from the first one. | X - Y |

* | to multiplies both operands. | X * Y |

/ | To divides the first operands by second one. | X / Y |

% | Modulus Operator Remainder. | X % Y |

++ | To increment operator by 1. | X++ |

-- | To decrement operator by 1. | X-- |

### Example of Arithmetic Operators in C++

#include <iostream> using namespace std; int main () { int x = 50; int y = 20; int output; output = x + y; cout << " '+' Operator example. The value of output is " << output << endl; output = x - y; cout << " '-' Operator example. The value of output is " << output << endl; output = x * y; cout << " '*' Operator example. The value of output is " << output << endl; output = x / x; cout << " '/' Operator example. The value of output is " << output << endl; output = x % y; cout << " '%' Operator example. The value of output is " << output << endl; output = x++; cout << " '++' Operator example. The value of output is " << output << endl; output = x--; cout << " '--' Operator example. The value of output is " << output << endl; return 0; }

**Output**

‘+’ Operator example. The value of output is 70

‘-‘ Operator example. The value of output is 30

‘*’ Operator example. The value of output is 1000

‘/’ Operator example. The value of output is 1

‘%’ Operator example. The value of output is 10

‘++’ Operator example. The value of output is 50

‘–‘ Operator example. The value of output is 51

## Rational Operators

Operator | Description | Example |
---|---|---|

To checks if the values of two operands are equal or not. | x == y | |

!= | To checks if the values of two operands are equal or not. | x != y |

> | To checks if the value of the left operand is greater than the value of the right operand. | x>y |

< | To checks if the value of the left operand is less than the value of the right operand. | x |

>= | To checks if the value of the left operand is greater than or equal to the value of the right operand. | x>=y |

<= | To checks if the value of the left operand is less than or equal to the value of the right operand. | x<=y |

### Example of Rational Operations in C++

#include <iostream> using namespace std; int main() { int x = 25; int y = 10; int c ; if( x == y ) { cout << "x is equal to y" << endl ; } if( x < y ) { cout << "x is less than y" << endl ; } if( x > y ) { cout << "x is greater than y" << endl ; } x = 10; y = 25; if( x <= y ) { cout << "x is less than or equal to y" << endl ; } if( y >= x ) { cout << "y is greater than or equal to x" << endl ; } return 0; }

**Output**

x is greater than yx is less than or equal to yy is greater than or equal to x

## Logical Operators

Logical Operators are used to determining logic operations between variables or values.

Operator | Description | Example |
---|---|---|

&& (logical AND) | It returns true if both conditions are true | (x>5)&&(y<5) |

|| (logical OR) | It returns true if at-least one of the condition is true | (x>=10)||(y>=10) |

! (logical NOT) | It reverses the state of the operand “((x>5) && (y<5))” | !((x>5)&&(y<5)) |

### Example of Logical Operators in C++

#include <iostream> using namespace std; int main() { int x = 10; int y = 40; int c; if (x && y) { cout << "Condition is true" << endl; } if (x || y) { cout << "Condition is true" << endl; } x = 0; y = 5; if (x && y) { cout << "Condition is true" << endl; } else { cout << "Condition is not true" << endl; } if (!(x && y)) { cout << "Condition is true" << endl; } return 0; }

**Output**

Condition is trueCondition is trueCondition is not trueCondition is true

## Bitwise Operators

Bitwise Operators are used for data manipulation at the bit level.

Operator | Description | Example |
---|---|---|

& | "AND" Operator copies a bit to the result if it exists in both operands. | (A & B) |

| | "OR" Operator copies a bit if it exists in either operand. | (A | B) |

^ | "XOR" Operator copies the bit if it is set in one operand but not both. | (A ^ B) |

~ | One's Complement Operator is unary and has the effect of 'flipping' bits. | (~A ) |

<< | Left Shift Operator. The left operand's value is moved left by the number of bits specified by the right operand. | A << 2 |

>> | Right Shift Operator. The left operand's value is moved right by the number of bits specified by the right operand. | A >> 2 |

### Example of Bitwise Operators in C++

#include <iostream> using namespace std; int main() { unsigned int x = 3; // 3 = 11 in bit unsigned int y = 4; // 4 = 100 in bit int result; result = x & y; cout << "x & y is: " << result << endl; result = x | y; cout << "x | y is: " << result << endl; result = x ^ y; cout << "x ^ y is: " << result << endl; result = ~x; cout << "~x is: " << result << endl; result = x << 2; cout << "x << 2 is: " << result << endl; result = x >> 2; cout << "x >> 2 is: " << result << endl; return 0; }

**Output**

x & y is: 0x | y is: 7x ^ y is: 7~x is : -4x << 2 is: 12x >> 2 is: 0

## Miscellaneous Operators

In addition to the above, C++ support the following operators:

### Ternary Operators

The Ternary Operator returns one value if the condition is true. Otherwise, it will return another value.

The syntax of Ternary Operators is

Expression1? Expression2: Expression3;

For example

var = (x < 5) ? 10 : 12;

If x is less than 5, the var value is set to 10. Else, it will be set to 12.

### sizeof Operator

A very useful operator that returns the size of a variable, depending on its Data Type, or the size of the Data Type itself.

int a; cout sizeof (a); cout sizeof (int);

### cast Operator

The cast operator is an operator that converts a Data Type into another.

double x = 3.14; int c ; c = (int) a;

## Points to remember

- Type of operators are Assignment, Arithmetic, Relational, Logical, and

Bitwise Operators. - C++ supports ternary sizeof and cast Operators as well.

Blog Hero