Operators in C++

In this tutorial, we will discuss different types of operators in C++. C++ consists of many operators for different operations. We need operators for carrying out many arithmetic and logical operations on the given input and produce the desired result.

Operators in C++

Operators in C++ can be divided into the following categories.

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators

These operators are used in our program to do required operations on the inputs and produce valid outputs.

Arithmetic Operators

Arithmetic operators are used in mathematical operations such as addition, subtraction, division, multiplication, etc. They are as follows:

+: binary operator to add two values or variables.

– : binary operator to subtract a value or a variable from another.

* : binary operator to multiply two values or variables.

/ : binary operator to divide a value or variable from another. It returns the quotient.

% : binary operator to return the remainder from a division operation.

++ : This is unary operator. It is used to increment the value of a variable.

— : Unary Operator which is used to decrement the value of a variable.

Read more about increment and decrement operators here: Increment (++) and Decrement (–) Operator in C++

Example code:

#include <iostream>
using namespace std;

int main()
{
  int a=3,b=2;
  cout<<"a+b="<<a+b<<endl;
  cout<<"a-b="<<a-b<<endl;
  cout<<"axb="<<a*b<<endl;
  cout<<"a/b="<<a/b<<endl;
  cout<<"a%b="<<a%b<<endl;
  cout<<"++a="<<++a<<endl;
  cout<<"--b="<<--b<<endl;
  return 0;
}

Output:

a+b=5
a-b=1
axb=6
a/b=1
a%b=1
++a=4
--b=1

Relational Operators

Relational Operators are used for comparing two values. They are as follows:

== : binary operator to check for equality of two operands. If the two operands are equal, it returns true(1).

!= : binary operator to check if two operands are not equal. It returns true(1) for unequal operands.

< : This is the binary operator that can check if the first operand is less than the second operand. If so, the operator returns true(1).

> : binary operator to check if the first operand is greater than the second one. If so, the operator will return true(1).

<= : binary operator to check if the first operand is less than or equal to the second operand. If so, this operator will return true(1).

>= : binary operator to check if the first operand is greater than or equal to the second operand. If so, it returns true(1).

Example code:

#include <iostream>
using namespace std;

int main()
{
  int a=3,b=2;
  //prints 1 if the condition is true else prints zero
  cout<<"a==b:"<<(a==b)<<endl;
  cout<<"a!=b:"<<(a!=b)<<endl;
  cout<<"a<b:"<<(a<b)<<endl;
  cout<<"a>b:"<<(a>b)<<endl;
  cout<<"a<=b:"<<(a<=b)<<endl;
  cout<<"a>=b:"<<(a>=b)<<endl;
    
  return 0;
}

Output:

a==b:0
a!=b:1
a<b:0
a>b:1
a<=b:0
a>=b:1

Logical operators

Logical operators in C++, do logical operations like AND, OR, NOT, etc. These are binary operators and work on two operands. They are as follows:

&& : It is a binary operator and returns true (1) when both operands are non-zero i.e. true.

|| : It is also a binary operator and return returns true if either of the two operands is non-zero i.e. true.

! : It is a unary operator and returns false(0) if the operand is non-zero i.e. true otherwise it returns true(1).

Example code:

#include <iostream>
using namespace std;

int main()
{
  int a=3,b=0;
  //prints 1 for true else prints zero
  cout<<"a&&b :"<<(a&&b)<<endl;
  cout<<"a||b :"<<(a||b)<<endl;
  cout<<"!a :"<<(!a)<<endl;
    
  return 0;
}

Output:

a&&b :0
a||b :1
!a :0

Bitwise Operator

Bitwise operators perform bit by bit operation on the operands. They are as follows:

& : This is bitwise AND. It is a binary operator. It does AND operation on bits i.e. copies 1 in the result if both bits are 1 otherwise 0.

| : This is bitwise OR. It is also a binary operator. It does OR operation on bits i.e. copies 1 in the result if either of the bits is 1 otherwise 0.

~ : This is bitwise Ones Complement Operator. It is unary operator. It copies 1 in the result if the bit is 0 otherwise 1.

^ : This is bitwise XOR. It is a binary operator and copies 1 in the result if both the bits are different.

<< : This is left shift operator. It is a binary operator. It shifts all the bits of the first operand to left by the number of bits given in the second operand.

>> : This is right shift operator. It is also a binary operator. It shifts all the bits of the first operand to right by the number of bits given in the second operand.

For example, if a=5 i.e. (0000 0101 in binary form) and b=35 (0010 0011 in binary form), then:

a & b:

0000 0101

0010 0011

————-

0000 0001

result=1

a | b:

0000 0101

0010 0011

————-

0010 0111

result=39

~a:

0000 0101

————-

1111 1010

result=-6

a ^ b:

0000 0101

0010 0011

————-

0010 0110

result=38

a<<2:

0000 0101

————-

0001 0100

result=20

a>>2:

0000 0101

————-

00000001

result=1

See the example code here.

#include <iostream>
using namespace std;

int main()
{
  int a=5,b=35;
  cout<<"a&b :"<<(a&b)<<endl;
  cout<<"a|b :"<<(a|b)<<endl;
  cout<<"~a :"<<(~a)<<endl;
  cout<<"a^b :"<<(a^b)<<endl;
  cout<<"a<<2 :"<<(a<<2)<<endl;
  cout<<"a>>2 :"<<(a>>2)<<endl;
  
  return 0;
}

 

Output:

a&b :1
a|b :39
~a :-6
a^b :38
a<<2 :20
a>>2 :1

To know more about bitwise operators: Bitwise Operators in C or C++

Assignment operators

Assignment operators assign some value to a variable. C++ assignment operators are as follows:

= : It is a binary operator. It assigns the value of the second operand to the first operand.

=+ : It is also a binary operator and assigns the sum of both operands to the first operand.

-= : It subtracts the second operand from the first operand and assigns the result to the first operand.

*= : This operator stores the product of both operands in the first operand.

/= : This operator divides the first operand by the second operand and stores the result in the first operand.

%= : This operator stores the remainder of the division of the first operand by the second operand in the first operand.

<<= and  >>=: The operator first does the left/right shift operation and then stores the result in the first operand.

&= and |= and ^= : The operator first does the bitwise AND/OR/XOR operation and then stores the result in the first operand.

See the example code.

#include <iostream>
using namespace std;

int main()
{
  int a,b=5;
  cout<<"b is "<<b<<endl;
  a=b;
  cout<<"a after a=b is "<<a<<endl;
  a+=b;
  cout<<"a after a+=b is "<<a<<endl;
  a-=b;
  cout<<"a after a-=b is "<<a<<endl;
  a*=b;
  cout<<"a after a*=b is "<<a<<endl;
  a/=b;
  cout<<"a after a/=b is "<<a<<endl;
  a%=b;
  cout<<"a after a%=b is "<<a<<endl;
  a<<=2;
  cout<<"a after a<<=b is "<<a<<endl;
  a>>=2;
  cout<<"a after a>>=b is "<<a<<endl;
  a&=b;
  cout<<"a after a&=b is "<<a<<endl;
  a|=b;
  cout<<"a after a|=b is "<<a<<endl;
  a^=b;
  cout<<"a after a^=b is "<<a<<endl;
  
  return 0;
}

Output:

b is 5
a after a=b is 5
a after a+=b is 10
a after a-=b is 5
a after a*=b is 25
a after a/=b is 5
a after a%=b is 0
a after a<<=b is 0
a after a>>=b is 0
a after a&=b is 0
a after a|=b is 5
a after a^=b is 0

Other Operators

Apart from the operators discussed so far, C++ provides us with some special operators that are as follows:

  • Conditional Operator: This is also called the ternary operator because it takes three operands for its operation. If the first operand is non-zero i.e. true, it would return the second operand otherwise it would return the third operand. Read more: Ternary Operator in C++
  • sizeof Operator: It is a unary operator and is used to find the size of the operand.
  • Comma Operator:  Comma operator returns the value of the last operation from a sequence of operations that are separated by a comma. The rest of the values are discarded. Comma Operator has the lowest precedence.

Operator precedence in C++

Operator precedence determines which part of an expression is evaluated first. Those with greater precedence will be calculated first. Associativity of an operator determines the order of the evaluation of the expression containing operators with the same precedence. The below operator precedence chart has been prepared such that the operators at the top have greater precedence than that of operators below them.

Operator Associativity
() [] ++(postfix) –(postfix) left to right
++(prefix) –(prefix) +(unary) -(unary) ! ~ sizeof right to left
* / % (Arithmetic) left to right
+ – (Arithmetic) left to right
<< >> (Shift operators) left to right
< > <= >= (Relational) left to right
== != (Relational) left to right
& (Bitwise) left to right
^(Bitwise) left to right
|(Bitwise) left to right
&&(Logical) left to right
||(Logical) left to right
?:(Conditinal Operator) right to left
= += -= *= /= %= <<= >>= &= |= ^= (Assignment operators) right to left
, (Comma) left to right

Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *