Operators in C++:

Operators perform operations on variables or constant data values like addition, multiplication, subtraction, division etc.
On the basis of number of operands, there are two types of operators:
  • Unary Operator - works on one operand, e.g. a++.
  • Binary Operator - works on two operands, e.g. a+b.

Note: You can add constant values to a variable.


operators.cpp
#include <iostream>
using namespace std;

int main(){
    int a = 10;
    cout<< a + 10 << endl;

    int b = 10 + 10;
    cout<<b<<endl;
    return 0;
}

Types of Operators:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators

We are going to discuss about these operators in detail.

Arithmetic Operators:

They are used to do Arithmetic Operations on data.
OperatorNameExampleDescription
+Addition
int a = 10 + 20;
Adds two data values.
-Subtraction
int a = 100 - 20;
Subtracts two data values.
*Multiplication
int a = 3 * 2;
Multiply two data values.
/Division
int a = 10 / 5;
Divides two data values.
%Modulus
int a = 23 % 5;
Return remainder when two data values are divided.
++Increment
int a = 10;  
a++;
Increment value of a variable by 1.
--Decrement
int a = 10;  
a--;
Decrement value of a variable by 1.

Increment:

  • Post-increment - it adds value at the end of instruction.
  • Pre-increment - it adds value at the start of instruction.



Assignment Operators:

They are used to assign values to variables. int a =10;
OperatorExampleDescription
=int a = 10;Assigning 10 to int type variable a.
+=a+=10;It is short form of "a=a+10".
-=a-=10;It is short form of "a=a-10".
*=a*=10;It is short form of "a=a*10".
/=a/=10;It is short form of "a=a/10".
%=a%=10;It is short form of "a=a%10".


Comparison Operators:

They are used to compare values and make decisions. They are most widely used in conditional statements. They return either true or false (Boolean), on the basis of which we proceed further.
e.g, For example, if 10 is greater than 5 then add a and b otherwise subtract a and b.

operators.cpp
#include <iostream>
using namespace std;

int main(){
    int a=10;
    int b=5;
    bool ans = a>b;
    cout<<ans; 
    return 0;
}
OperatorExampleDescription
==a == b;Double equal checks if a is equal to b.
!=a != b;Not equal checks if a is not equal to b.
a < b
a < b
Less than checks if a is less than b.
a <= b
a <= b
Less than or equal checks if a is less than or equal b.
a > b
a > b
Greater than checks if a is greater than b.
a >= b
a >= b
Greater than or equal checks if a is greater than or equal b.


Logical Operators:

Like comparison operators, they also return true (1) or false (0).
There are three types of logical operators:
OperatorExampleDescription
&&
(a > b) && (b > c)
AND - it says that both conditions should be true. a should be greater than b and b should be greater than c.
||
(a > b) || (b > c)
OR - it says that only one conditions should be true. a should be greater than b or b should be greater than c.
!
!(a > b)
NOT - inverts the result. If a is greater than b, it will return false.


Bitwise Operators:

Bitwise operators perform operation on bit level. We deal with decimal values with base 10, but when using bitwise operators, computer convert base 10 to base 2 (binary format) and perform operations on bit and again convert them to base 10 and show us the output in decimal form.
OperatorNameExampleDescription
&bitwise ANDa & bBinary of 2 is 10 and of 3 is 11 and their and will be 10 in binary and computer gives the answer 2.
|bitwise ORa | bBy taking bitwise OR of 10 and 11 we will get 11 in binary and output will be 3.
~bitwise NOT~a (uniary operator)Binary of a (a=2) is 10 and bitwise not will be 01 and output will be 1.
>>
Shift Right
a >> 2
a=8; binary is 1000.
a << 2
will be 10 and output will be 2.
<<
Shift Left
a << 2
a=8; binary is 1000.
a >> 2
will be 100000 and output will be 32.


Operator Precedence:

If we have multiple operators in an expression, then compiler will have to decide which operator will be evaluated first and which will be evaluated in the last.
The order in which the arithmetic expression is evaluated is called the order of precedence. It is also known as hierarchy of operation.
When an arithmetic expression is evaluated, the computer performs only one operation at one time. In an expression in C++ the operations are performed in the following order
  • All multiplications and divisions are performed first from left to right.
  • All additions and subtractions are then performed from left to right.
  • If the parenthesis are used in an expression, the expression within parenthesis are first computed from left to right.
  • When parenthesis are used within parenthesis, the expression within innermost parenthesis is evaluated first.
You can check out precedence of operators at:
Example:
(4-(3*5))+2
1. (3*5) is computed and returns value of 15.
2. 4-15 is computed and then return a value of -11.
3. -11+2 is computed and returns value of -9.