C++ Operators

Introduction

Generally, an expression consists of two entities – operators and operands. 

Operators are used to performing an operation between the given set of operands.

For example,

2+3=5

Here 2 and 3 are operands and + works like an operator which adds the two operands to give 5 as an output.

C++ is loaded with a vast number of operators which we shall discuss here. Those are:- 

  1. Arithmetic Operators
  2. Assignment Operators
  3. Relational Operators
  4. Bitwise Operators
  5. Logical Operators

Now we shall discuss each type in detail.

Arithmetic Operators

These sets of operators can be used to perform various mathematical operations, like finding the sum, difference, product, quotient, remainder, etc.

The following table shows the operator symbol and functionality.

OperatorFunctionality
+Addition/Sum
Subtraction/Difference
*Multiplication/Product
/Division/Quotient
%Modulo/Remainder

The following code explains the usage of each operator in detail

Code:

C++ Code

#include <bits/stdc++.h>
using namespace std;

int main() {
  int a = 4, b = 2;
  cout << "Sum is: " << a + b << "\n";
  cout << "Difference is: " << a - b << "\n";
  cout << "Product is: " << a * b << "\n";
  cout << "Quotient is: " << a / b << "\n";
  cout << "Remainder is: " << a % b << "\n";

  return 0;
}

Output:

Sum is: 6
Difference is: 2
Product is: 8
Quotient is: 2
Remainder is: 0

Assignment Operators

Assignment operators can be used as a shorthand to perform an arithmetic operation and assign the value to another variable. 

The following table shows the operator’s symbol and functionality

OperatorFunctionality
=a=5;
+=a=a+5;
-=a=a-5;
*=a=a*5;
%=a=a%5;

The following code explains the usage in detail

Code:

C++ Code

#include <bits/stdc++.h>
using namespace std;

int main() {
  int a, b, c, d, e;
  
  a = 5, b = 3, c = 4, d = 5, e = 6;
  cout << "Value of a: " << a << "\n";
  
  b += 4;
  cout << "Value of b: " << b << "\n";
  
  c -= 1;
  cout << "Value of c: " << c << "\n";
  
  d /= 5;
  cout << "Value of d: " << d << "\n";
  
  e %= 2;
  cout << "Value of e: " << e << "\n";

  return 0;
}

Output:

Value of a: 5
Value of b: 7
Value of c: 3
Value of d: 1
Value of e: 0

Relational Operators

These operators can be used to detect a relation between two operands.  If a certain relation exists, TRUE is returned, else FALSE is returned.

The following table depicts the operator’s symbol and functionality

OperatorFunctionality
a == bChecks if a is equal to b
a != bChecks if a is not equal to b
a > bChecks if a is greater than b
a < bChecks if a is smaller than b
a >= bChecks if a is greater or equal to b
a <= bChecks if a is smaller or equal to b

The following code explains the usage in detail

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;

int main() {
  int a = 3, b = 5;
  int res = 0;

  res = a == b;
  cout << "Is a==b? " << res << "\n";

  res = a != b;
  cout << "Is a!=b? " << res << "\n";

  res = a > b;
  cout << "Is a>b? " << res << "\n";

  res = a < b;
  cout << "Is a<b? " << res << "\n";

  res = a >= b;
  cout << "Is a>=b? " << res << "\n";

  res = a <= b;
  cout << "Is a<=b? " << res << "\n";
  return 0;
}

Output:

Is a==b? 0
Is a!=b? 1
Is a>b? 0
Is a<b? 1
Is a>=b? 0
Is a<=b? 1

Logical Operators

They can be used to make decisions if there exist multiple deciding expressions.

The following table contains the operator symbols and functionality

OperatorFunctionality
&&Exp1 && Exp2 returns true if both the expression return true
|| Exp 1 && Exp2, returns true if both or either of the two expressions returns true
!!Exp, returns true if the expression returns false and vice versa.

The following code explains the working of the logical operators

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;

int main() {
  int a = 2, b = 3;
  int res = 0;
  res = (a > b) && (a == b);
  cout << res << "\n";

  res = (a != b) || (a >= b);
  cout << res << "\n";

  res = !(a < b);
  cout << res << "\n";
  
  return 0;
}

Output:

0
1
0

Bitwise Operators

Bitwise operators can be used to perform operations at the bit level.

The following table gives the operator symbol and functionality

OperatorFunctionality
&AND: Performs AND operation b/w two operands
|OR: Performs OR operation b/w two operands
^XOR: Performs XOR operation b/w two operands
<<LEFT SHIFT: Performs LEFT SHIFT operation on 1 operand
>>RIGHT SHIFT: Performs RIGHT SHIFT Operation on 1 operand

Since programmers find it difficult to understand these operators, we’ll discuss them in detail.

AND operator (&)

AND operator returns 1 only when both the bits are set i.e. equal to 1 in the numbers.

The truth table for the AND operator looks as follows

ABA & B
000
010
100
111

Let’s take an example,

Suppose we are trying to find the 3 & 4. The 4-bit representation of 3 and 4 would look as follows
3: 0011
4: 0100

When we perform the AND operation using the truth table above, we get 0000 which corresponds to 0.

The following code shows the working of AND operator.

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;

int main() {
  int a = 3, b = 4;
  int res = a & b;
  cout << res << "\n";
  return 0;
}

Output: 0

OR Operation ( | )

OR Operation returns 1 if either of the two or both bits are set i.e. equal to 1. The truth table for OR looks as follows

ABA | B
000
011
101
111

Let’s take an example,

Suppose you are trying to find 5 | 6. The 4-bit representation of 5 and 6 would be as follows
5: 0101
6: 0110

On performing the OR operation, we would get 0111 as the result which would correspond to 7.

The following code explains the usage of the OR operator

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;

int main() {
  int a = 5, b = 6;
  int res = a | b;
  cout << res << "\n";
  return 0;
}

Output: 7

XOR Operator (^)

XOR Operator returns 1 only when both bits are conjugate to each other i.e. if the first bit is 1 and the second bit is 0 and vice versa. The truth table of the XOR operator looks as follows

ABA ^ B
000
011
101
110

For example

You need to find 9 ^ 3. The 4-bit representation of 9 and 3 would look like
9: 1001
3: 0011

On performing the XOR operation, we would get 1010 as a result which would correspond to 10.

The following code depicts the usage of the XOR operator

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;

int main() {
  int a = 9, b = 3;
  int res = a ^ b;
  cout << res << "\n";
  return 0;
}

Output: 10

LEFT SHIFT OPERATOR (<<)

It can be used to shift bits to the left up to a certain number of significant bits. 

For example

You need to perform 3 << 1
Representing 3 in terms of 4 bit binary system, we get
3: 0011

When we shift the bits by 1 to the left, we end up getting 0110 as the result which would correspond to 6

Let’s check out the code for this

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;

int main() {
  int a = 3;
  int res = a << 1;
  cout << res << "\n";
  return 0;
}

Output: 6

RIGHT SHIFT Operator (>>)

It can be used to shift the bits to the right up to a certain number of significant bits.

For example,

You need to perform 12 >> 1
Representing 12 in terms of 4 bit binary system, we get
12: 1100

When we shift the bits to the right, we end up getting 0110 as the result which would correspond to 6.

Let’s check out the code for this

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;

int main() {
  int a = 12;
  int res = a >> 1;
  cout << res << "\n";
  return 0;
}

Output: 6

Special thanks to Yash Mishra for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article