Operators in C++

An operator is a symbol.

It is used to tells the compiler to perform specific mathematical or logical functions.

C++ language have a following types of built-in operators:−

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

 

Arithmetic Operators

C++ language have a following arithmetic operators.

Assume variable i holds 100 and variable j holds 200 then –

Operator Description Example
+ Adds two operands. i + j = 300
Subtracts second operand from the first. i − j = -100
* Multiplies two operands. i * j = 20000
/ Divide. j / i = 2
% Modulus Operator returns the remainder of integer division. j % i = 0
++ Increment operator increases the value of variable by one. i++ = 101
Decrement operator decreases the value of variable by one. i– = 99

Example : Write a C++ program to demonstrate the various arithmetic operators.

#include 
using namespace std;
int main()
{
	int a = 10;
	int b = 25;
	int c, d, e, f;
	c = a + b;	// add value of variable a&b and store in variable c (c=35)	
	d = b - a;	// subtract value of variable a from b and store in variable d (d=15)
	e = b / a;	// e=2			
	f = b % a;	//f=5

	cout << "\n c = " << c;
	cout << "\n d = " << d;
	cout << "\n e = " << e;
	cout << "\n f = " << f;
	return 0;
}

Output

 c = 35
 d = 15
 e = 2
 f = 5

Increment and Decrement Operator

  1. Increment operator      ++
  2. Decrement operator    --

In a C++ language, Increment operator (++)  increases the value of variable by one and Decrement operator  (--) decreases the value of variable by one.

There are two form of increment  and decrement operator:

  1. Prefix form
  2. Postfix form

Prefix Form : In the prefix expression operator appears in the expression before the operands.

Example : ++A 

In the prefix form first the value of operand is increment or decrement than the value of operand is used in expression.

For Example: int  i = 10;
      j = ++i ;    // j=11 , i=11

Postfix Form: In the postfix expression operator appears in the expression after the operands.

Example : A++

In the postfix form first the value of operand is used in expression than value of operand is incremented or decremented.

For Example: int  i = 10;
    j = i++ ;    // j=10, i=11

Example: Write a C++ program to demonstrate the increment and decrement operator. 

#include 
using namespace std;
int main()
{
	int a = 10;
	int b, c;
	b = ++a;	// b=11, a=11
	c = a++;	//c= 11, a=12	

	cout << "\n a = " << a;
	cout << "\n b = " << b;
	cout << "\n c = " << c;
	return 0;
}

Output

a = 12
 b = 11
 c = 11

Relational Operators

C++ Language has a following relational operators.

For example: Assume variable A holds 100 and variable B holds 200 then −

Operator Description Example
== Equals to (A == B) is not true.
!= Not equal (A != B) is true.
Greater than (A > B) is not true.
Less than (A < B) is true.
>= Greater than or equals to (A >= B) is not true.
<= Less than or equals to (A <= B) is true.

Example:  Write a C++ Program to find the largest of two numbers.

#include 
using namespace std;
int main()
{
	int a = 10;
	int b = 50;

	if (a > b)
	{
		cout << "largest value = " << a;
	}
	else
	{
		cout << "largest value = " << b;
	}

	return 0;
}

Output

largest value = 50

Logical Operators

There are three basic logical operators available in C++ language.

Assume variable A and B holds 1 and 0 respectively then –

Operator Description Example
&&  AND operator (the condition becomes true if both the operands are non-zero) (A && B) is false.
||  OR Operator (the condition becomes true if any of the two operands is non-zero) (A || B) is true.
!  NOT Operator (this operator reverse the logical state of operand. For example: If condition is true, then NOT operator will make it false) !(A && B) is true.

Example:  Write a C++ Program to find the largest among three numbers using AND logical operator.

#include 
using namespace std;
int main()
{
	int a, b, c;
	a = 50;
	b = 100;
	c = 20;
	if (a > b && a > c)
	{
		cout << " largest number is " << a;
	}
	else
	{
		if (b > c && b > a)
			cout << " largest number is " << b;
		else
			cout << " largest number is " << c;
	}

	return 0;
}

Output

largest number is 100

Bitwise Operators

In a C++ programming language Bitwise operator works on bits and perform bit-by-bit operation.

There are following bitwise operators in C++:-

Operator Description
& Binary AND Operator
| Binary OR Operator
^ Binary XOR Operator
~ Binary One's Complement Operator is unary
<<  Binary Left Shift Operator
>>  Binary Right Shift Operator

The truth tables for &, |, and ^ is as follows –

a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume A = 60 and B = 13 in binary format, they will be as follows −

               A in binary     = 0011 1100         (60)                     

              B in binary     = 0000 1101          (13)

                               -----------------

Bitwise AND (&)

                      A      = 0011 1100         (60)                

          &         B      = 0000 1101          (13)

---------------------------------------------------------

                                0000 1100           (12)

Bitwise OR (|)

                      A      = 0011 1100         (60)                

          |          B      = 0000 1101          (13)

---------------------------------------------------------

                                0011 1101           (61)

Bitwise XOR (^)

                      A      = 0011 1100         (60)                

          ^         B      = 0000 1101          (13)

---------------------------------------------------------

                                0011 0001           (49)

Binary Left Shift Operator

Left shift operator shift all the bits in a left direction to specified number of times.

Exapmle:

int i = 16;		// 0001 0000
i = i<<2;  		// 0100 0000  (shift left 2 bits, front 2 bits will be lost, and append 2 zero) 
cout<<“i =” << i;

Output:

i = 64

Binary Right Shift Operator

Left shift operator shift all the bits in a left direction to specified number of times.

Exapmle:

int i = 16;		// 0001 0000
i = i>>2;  		// 0000 0100  (shift right 2 bits, last 2 bits will be lost, and 2 zero add in front) 
cout<< “i =” << i;

Output

i=4

Exapmle:  Write a program to demonstrate Bitwise operator.

#include 
using namespace std;
int main()
{
	int a = 60;
	int b = 13;
	int c, d, e, f, g, h;
	c = ~a;	//c = -61
	d = a | b;	//d= 61	
	e = a &b;	// e= 12	
	f = a ^ b;	//f=  49

	cout << "\n c =" << c;
	cout << "\n d =" << d;
	cout << "\n e =" << e;
	cout << "\n f =" << f;

	g = h = 16;
	g = g << 2;	//g= 64	
	h = h >> 2;	// h=4;
	cout << "\n g =" << g;
	cout << "\n h =" << h;

	return 0;
}

Output

 c =-61
 d =61
 e =12
 f =49
 g =64
 h =4

Assignment Operators

In a C++  language there are following assignment operators  −

Operator Description
= assignment operator (it assigns values of right side operands to left side operand )
+= Add assignment operator
-= Subtract assignment operator
*= Multiply  assignment operator
/= Divide assignment operator
%= Modulus assignment operator
<<= Left shift assignment operator
>>= Right shift assignment operator
&= Bitwise AND assignment operator
^= Bitwise exclusive OR assignment operator
|= Bitwise inclusive OR assignment operator

Add Assignment Operator

Example:

int a = 5;	// now set the value of a = 5;
a+=4;		// add assignment operator add 4 with a’s value(5) and save new value(9) again in a. 
 cout<< “value of a =”<<, a);

Output

value of a = 9

Subtract Assignment Operator

Example:

int a = 15;	// now set the value of a = 5;
a-=4;		// subtract assignment operator subtract 4 from a’s value(15) and save new value(11) again in a. 
cout<< “value of a =”<<, a);

Output

value of a = 11

Multiply  assignment operator

Example:

int a = 5;	// now set the value of a = 5;
a*=4;		// multiply assignment operator, multiply 4 with  a’s value(5) and save new value(20) again in a. 
 cout<< “value of a =”<<, a);

Output

value of a = 20

The ?  Operator

This operator also known as conditional operator . It is similar to if-else statement.

Syntax of ? operator

 Expression1 ? Expression 2 : Expression 3;

There are three expressions in ? operator, Expression1, Expression2, and Expression3 .

The value of a ? expression is determined like this −

  • If Exp1 is true, then Exp2 is evaluated.
  • If Exp1 is false, then Exp3 is evaluated.

Example: write a program to find the grater between two number using ? operator.

#include 

using namespace std;
int main() {
        int a, b, c;
        a = 10;
        b = 20;
        c = (a > b) ? a : b;
        cout << "\n grater value = " << c;

        return 0;
}

Output:

grater
value = 20

Example: write a program to find the grater among three number using ? operator.

#include 

using namespace std;
int main() {
        int a, b, c, d;
        a = 160;
        b = 200;
        c = 30;

        d = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
        cout << "\n grater value = " << d;

        return 0;
}

Output

grater value = 200
Categories C++