Introduction
An operator is a symbol that operates on a value to perform specific mathematical or logical computations. They form the foundation of any programming language. In C++, we have built-in operators to provide the required functionality.
An operator operates the operands.
Ex:
1 | int c = a + b; |
Operators in C++ can be classified into 6 types:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Ternary or Conditional Operators
Arithmetic Operators
Name | Sign | Description | Example |
---|---|---|---|
Increment Operator | ++ | Increases the integer value of the variable by one | int a = 5; a++; // returns 6 |
Decrement Operator | — | Decreases the integer value of the variable by one | int a = 5; a–; // returns 4 |
Addition | + | Adds two operands | int a = 3, b = 6; int c = a+b; // c = 9 |
Subtraction | – | Subtracts second operand from the first | int a = 9, b = 6; int c = a-b; // c = 3 |
Multiplication | * | Multiplies two operands | int a = 3, b = 6; int c = a*b; // c = 18 |
Division | / | Divides first operand by the second operand | int a = 12, b = 6; int c = a/b; // c = 2 |
Modulo Operation | % | Returns the remainder an integer division | int a = 8, b = 6; int c = a%b; // c = 2 |
Note:
++a and a++, both are increment operators, however, both are slightly different.
In ++a, the value of the variable is incremented first and then It is used in the program. In a++, the value of the variable is assigned first and then It is incremented. Similarly happens for the decrement operator.
The Modulo operator(%) operator should only be used with integers.
Relational Operators
These operators are used for the comparison of the values of two operands.
Name | Sign | Description | Example |
---|---|---|---|
Is Equal To | == | Checks if both operands are equal | int a = 3, b = 6; a==b; // returns false |
Greater Than | > | Checks if first operand is greater than the second operand | int a = 3, b = 6; a>b; // returns false |
Greater Than or Equal To | >= | Checks if first operand is greater than or equal to the second operand | int a = 3, b = 6; a>=b; // returns false |
Less Than | < | Checks if first operand is lesser than the second operand | int a = 3, b = 6; a<b; // returns true |
Less Than or Equal To | <= | Checks if first operand is lesser than or equal to the second operand | int a = 3, b = 6; a<=b; // returns true |
Not Equal To | != | Checks if both operands are not equal | int a = 3, b = 6; a!=b; // returns true |
Logical Operators
These operators are used to combine two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false.
Name | Sign | Description | Example |
---|---|---|---|
AND | && | Returns true only if all the operands are true or non-zero | int a = 3, b = 6; a&&b; // returns true |
OR | || | Returns true if either of the operands is true or non-zero | int a = 3, b = 6; a||b; // returns true |
NOT | ! | Returns true if the operand is false or zero | int a = 3; !a; // returns false |
Bitwise Operators
These operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands.
Name | Sign | Description | Example |
---|---|---|---|
AND | & | Copies a bit to the evaluated result if it exists in both operands | int a = 2, b = 3; (a & b); //returns 2 |
OR | | | Copies a bit to the evaluated result if it exists in any of the operand | int a = 2, b = 3; (a | b); //returns 3 |
XOR | ^ | Copies the bit to the evaluated result if it is present in either of the operands but not both | int a = 2, b = 3; (a ^ b); //returns 1 |
Left Shift | << | Shifts the value to left by the number of bits specified by the right operand. | int a = 2, b = 3; (a << 1); //returns 4 |
Right Shift | >> | Shifts the value to right by the number of bits specified by the right operand. | int a = 2, b = 3; (a >> 1); //returns 1 |
One’s Complement | ~ | Changes binary digits 1 to 0 and 0 to 1 | int b = 3; (~b); //returns -4 |
Note:
Only char and int data types can be used with Bitwise Operators.
Assignment Operators
These operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.
Sign | Description | Example |
---|---|---|
= | Assigns the value on the right to the variable on the left | int a = 2; // a = 2 |
+= | First adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left | int a = 2, b = 4; a+=b; // a = 6 |
-= | First subtracts the value on the right from the current value of the variable on left and then assign the result to the variable on the left | int a = 2, b = 4; a-=b; // a = -2 |
*= | First multiplies the current value of the variable on left to the value on the right and then assign the result to the variable on the left | int a = 2, b = 4; a*=b; // a = 8 |
/= | First divides the current value of the variable on left by the value on the right and then assign the result to the variable on the left | int a = 4, b = 2; a /=b; // a = 2 |
Ternary or Conditional Operators(?:)
This operator returns the value based on the condition.
The ternary operator ? determines the answer on the basis of the evaluation of Expression1. If it is true, then Expression2 gets evaluated and is used as the answer for the expression. If Expression1 is false, then Expression3 gets evaluated and is used as the answer for the expression.
This operator takes three operands, therefore it is known as a Ternary Operator.