C++ Operators

 

C++ Operators

Special symbols that are used to perform actions or operations are known as operators. They could be both unary or binary.

For example, the symbol + is used to perform addition in C++ when put in between two numbers, so it is a binary operator. There are different types of operators. They are as follows:

 

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, etc. They could be both binary and unary. A few of the simple arithmetic operators are

 

Operation

Description

a + b

Adds a and b

a - b

Subtracts b from a

a * b

Multiplies a and b

a / b

Divides a by b

a % b

Modulus of a and b

a++

Post increments a by 1

a-- 

Post decrements a by 1

++a

Pre increments a by 1

--a

Pre decrements a by 1

 

Let’s see their implementation in C++.


#include <iostream>
using namespace std;
 
int main()
{
    int a = 4, b = 5;
    cout << "The value of a + b is " << a + b << endl;
    cout << "The value of a - b is " << a - b << endl;
    cout << "The value of a * b is " << a * b << endl;
    cout << "The value of a / b is " << a / b << endl;
    cout << "The value of a % b is " << a % b << endl;
    cout << "The value of a++ is " << a++ << endl;
    cout << "The value of a-- is " << a-- << endl;
    cout << "The value of ++a is " << ++a << endl;
    cout << "The value of --a is " << --a << endl;
}


Output:

The value of a + b is 9 
The value of a - b is -1
The value of a * b is 20
The value of a / b is 0 
The value of a % b is 4 
The value of a++ is 4   
The value of a-- is 5
The value of ++a is 5
The value of --a is 4

 

Relational Operators

Relational operators are used to check the relationship between two operands and to compare two or more numbers or even expressions in cases. The return type of a relational operator is a Boolean that is, either True or False (1 or 0).

 

Operator

Description

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

==

Is equal to

!=

Is not equal to

 

Let’s see their implementation in C++.


#include <iostream>
using namespace std;
 
int main()
{
    int a = 4, b = 5;
    cout << "The value of a == b is " << (a == b) << endl;
    cout << "The value of a < b is " << (a < b) << endl;
    cout << "The value of a > b is " << (a > b) << endl;
}


Output:

The value of a==b is 0
The value of a<b is 1
The value of a>b is 0

 

The output is 0 for a==b, since a and b are not equal and 1 for a<b, since a is less than b.

 

Logical Operators

Logical Operators are used to check whether an expression is true or false. There are three logical operators i.e. AND, OR, and NOT. They can be used to compare Boolean values but are mostly used to compare expressions to see whether they are satisfying or not. 


  • AND: it returns true when both operands are true or 1.

  • OR: it returns true when either operand is true or 1.

  • NOT: it is used to reverse the logical state of the operand and is true when the operand is false.

 

Operator

Description

&&

AND Operator

||

OR Operator

!

NOT Operator

 

Let’s see their implementation in C++.


#include <iostream>
using namespace std;
 
int main()
{
    int a = 1, b = 0;
    cout << "The value of a && b is " << (a && b) << endl;
    cout << "The value of a || b is " << (a || b) << endl;
    cout << "The value of !a is " << (!a) << endl;
}


Output:

The value of a && b is 0
The value of a || b is 1
The value of !a is 0

 

Bitwise Operators

A bitwise operator is used to perform operations at the bit level. To obtain the results, they convert our input values into binary format and then process them using whatever operator they are being used with. 

 

Operator

Description

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

~

Bitwise Complement

>>

Shift Right Operator

<<

Shift Left Operator

 

Let’s see their implementation in C++.


#include <iostream>
using namespace std;
 
int main()
{
    int a = 13; //1101
    int b = 5;  //101
    cout << "The value of a & b is " << (a & b) << endl;
    cout << "The value of a | b is " << (a | b) << endl;
    cout << "The value of a ^ b is " << (a ^ b) << endl;
    cout << "The value of ~a is " << (~a) << endl;
    cout << "The value of a >> 2 is " << (a >> 2) << endl;
    cout << "The value of a << 2 is " << (a << 2) << endl;
}


Output:

The value of a & b is 5
The value of a | b is 13
The value of a ^ b is 8
The value of ~a is -14
The value of a >> 2 is 3
The value of a << 2 is 52

 

Assignment Operators

Assignment operators are used to assign values. We will use them in almost every program we develop.

int a = 0;
int b = 1;


Equal to (=) is the assignment operator here. It is assigning 0 to a and 1 to b in the above example.

 

Operator

Description

=

It assigns the right side operand value to the left side operand.

+=

It adds the right operand to the left operand and assigns the result to the left operand.

-=

It subtracts the right operand from the left operand and assigns the result to the left operand.

*=

It multiplies the right operand with the left operand and assigns the result to the left operand.

/=

It divides the left operand with the right operand and assigns the result to the left operand.

 

Operator Precedence and Associativity

 

Operator precedence

It helps us determine the precedence of an operator over another while solving an expression. Consider an expression a+b*c. Now, since the multiplication operator's precedence is higher than the precedence of the addition operator, multiplication between a and b is done first and then the addition operation will be performed.

 

Operator associativity

It helps us to solve an expression; when two or more operators having the same precedence come together in an expression. It helps us decide whether we should start solving the expression containing operators of the same precedence from left to right or from right to left.

The table containing the operator precedence and operator associativity of all operators can be found here. C++ Operator Precedence - cppreference.com