The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

C++ Operators

Operators are used to perform mathematical & logical operations. Operators supported by C++ can be categorized into arithmetic, logical, relational, bitwise (used for bit manipulation) & some special operators. Let's see how these operators are used in programming.

Arithmetic Operators
Arithmetic Operators supported by C++ are ADD ( + ), SUBTRACT ( - ), MULTIPLY ( * ), DIVIDE ( / ),
MODULO ( % ), INCREMENT ( ++ ) and DECREMENT ( -- )
.
' / ' operator prints the quotient of the division operation & ' % ' operator prints the remainder of the division operation. Fo e.g, 5 / 2 = 2 and 5 % 2 = 1.
' ++ ' operator increments a value by 1 and ' -- ' operator decrements a value by 1. For e.g 5++ = 6 and 5-- = 4.
The behaviour of ' + ', ' - ' and ' * ' are trivial.
Let's look at the following program to see arithmetic operators in action :

/*
 * Demonstration of arithmetic operators
 */

#include<iostream>
using namespace std;

int main() {
   int a = 5, b = 2;
   int x, y, z, r, s, u, v;
   x = a + b; // addition ( Value of a+b is assigned to x )
   y = a - b; // subtraction
   z = a * b; // multiplication
   r = a / b; // division
   s = a % b; // modulo
   cout << " Sum of " << a << " and "<< b << " is " << x << endl;
   cout << " Difference of " << a << " and "<< b << " is " << y << endl;
   cout << " Product of " << a << " and "<< b << " is " << z << endl;
   cout << " Division of " << a << " by "<< b << " is " << r << endl;
   cout << " Modulo of " << a << " by "<< b << " is " << s << endl;
   cout << " Incrementing a " << ++a << endl;
   cout << " Decrementing b " << --b << endl;
   return 0;
}

Please note that some of the expressions in which a variable is present in both sides can be written as :

Expressionis Equivalent to
X = X + YX += Y
X = X - YX -= Y
X = X * YX *= Y
X = X / YX /= Y
X = X % YX %= Y

Logical Operators
Logical Operators supported by C++ are AND ( && ), OR ( || ) and NOT ( ! ).
Consider two operands A and B.
A && B evaluates to True only if both A and B are True.
A || B evaluates to True if any of the two operands is True.
' ! ' operator negates the state of an operand. For e.g, if A = True, then !A = False.
Note that any non-zero value is considered True and zero is considered False.
Following program shows logical operators in action :

/*
 * Demonstration of logical operator
 */

#include<iostream>
using namespace std;

int main() {
   bool a = true, b = false;
   int x, y, z;
   x = a && b; // x = false
   y = a || b; // y = true
   z = !b;     // z = true
   cout << a << " AND " << b << " = " << x << endl;
   cout << a << " OR " << b << " = " << y << endl;
   cout << "NOT " << b << " = " << z << endl;
   return 0;
}

Relational Operators
Relational operators ( used for comparison operations ) supported by C++ are EQUAL ( == ),
NOT EQUAL ( != ), GREATER THAN ( > ), LESS THAN ( < ), GREATER THAN OR EQUAL TO ( >= ) and LESS THAN OR EQUAL TO ( <= )
.
Following program illustrates the behaviour of relational operators :

/*
 * Demonstration of Relational Operators
 */

#include<iostream>
using namespace std;

int main() {
   int a = 5, b = 2, c = 2;
   int x, y, z, r;
   x = ( b == c ); // x = 1 (true) since b is equal to c
   y = ( a != b ); // y = 1 (true) since a is not equal to b
   z = ( b < c ); // z = 0 (false) since b is not less than c
   r = ( b <= c ); // r = 1 (true) since b is not less but equal to c
   cout << " x : "<< x << endl;
   cout << " y : "<< y << endl;
   cout << " z : "<< z << endl;
   cout << " r : "<< r << endl;
   return 0;
}

Bitwise Operators
Bitwise Operators ( used for bit manipulation ) supported by C++ are BITWISE AND ( & ), BITWISE OR ( | ), BITWISE XOR ( ^ ), BITWISE NOT ( ~ ), LEFT SHIFT ( << ) and RIGHT SHIFT ( >> ). These operators operate at bit level.
Consider two bits bit1 and bit2.
bit1 & bit2 evaluates to True if and only if both bit1 and bit2 are True.
bit1 | bit2 evaluates to True if any of the two bits is True.
bit1 ^ bit2 evaluates to True if and only if one of the bits is True.
~ operator toggles a bit. If bit1 = True, the ~bit1 = False.
' A << 2 ' left shifts the bits of a number (A) by 2 positions.
' A >> 1 ' right shifts the bits of a number (A) by 1 position.
Suppose A = 7 and B = 19. In binary format : A = 00000111 and B = 00010011
Following table shows how bitwise operators do bit manipulation :

Binary
A00000111
B00010011
A & B00000011
A | B00010111
A ^ B00010100
~A11111000
A << 200011100
A >> 100000011

Following program demonstrate the behaviour of bitwise operators :

/*
 * Demonstration of bitwise operators
 */

#include<iostream>
using namespace std;

int main() {
   int a = 7, b = 19;
   int x, y, z, r, s;
   x = a & b;  // 7 & 19 = (00000111 & 00010011) = 00000011 = 3
   y = a | b;  // 7 | 19 = (00000111 | 00010011) = 00010111 = 23
   z = a ^ b;  // 7 ^ 19 = (00000111 ^ 00010011) = 00010100 = 20
   r = a << 2; // 7 << 2 = (00000111 << 2) = 00011100 = 28
   s = a >> 1; // 7 >> 1 = (00000111 >> 1) = 00000011 = 3
   cout << a << " & " << b << " = " << x << endl;
   cout << a << " | " << b << " = " << y << endl;
   cout << a << " ^ " << b << " = " << z << endl;
   cout << a << " << " << 2 << " = " << r << endl;
   cout << a << " >> " << 1 << " = " << s << endl;
   return 0;
}

Note that in the above program, there is no demonstration of NOT ( ~ ) operator since the result would be unexpected and will confuse you. You might have noticed that all decimal numbers are represented using 8 bits
( 1 Octet ) but in the system memory it is stored as a sequence of multiple octets (4 in case of 32 bit systems). In our case, other 3 octets would be all 0s. But NOT operation would toggle all those 0s to 1s which would result into a very high value ( outside the range of int ). As a result, some unexpected value would be displayed.

Operators for taking user input and displaying output
Please note that " << " is also used for printing output and " >> " is also used for taking input from user at runtime. Here is a simple program which takes a no. as input and prints its multiplication table :

/*
 * Multiplication table 
 */

#include<iostream>
using namespace std;

int main() {
   int x;
   cout << "Enter any number :: ";
   cin >> x; // taking user input
   cout << endl << "Multiplication Table :- " << endl;
   cout << x << " * 1 = " << x*1 << endl;
   cout << x << " * 2 = " << x*2 << endl;
   cout << x << " * 3 = " << x*3 << endl;
   cout << x << " * 4 = " << x*4 << endl;
   cout << x << " * 5 = " << x*5 << endl;
   return 0;
}
Run this program in your system to take input at run-time  

Special Operators
There are some special characters used for specific purpose in C++ :

sizeofIt is used to get the size of a data type i.e the memory space required by a variable of a particular data type.
ConditionConditional Operator returns a value depending on a condition.
Syntax : ( Condition ? a : b ). If condition is true, a is returned else b is returned.
Scope ResolutionIt is used to differentiate between local and global variables with the same name.
AssignmentIt is used to assign one value to other in an expression.
In the expression c = a + b, value of ( a + b ) is assigned to c.

Refer to the section on C++ Tokens for sample program demonstrating sizeof and Variable Scope for scope resolution operator. Following program illustrates the use of conditional operator :

/*
 * Demonstrating Conditional Operator
 */

#include<iostream>
using namespace std;

int main() {
   int a = 5, b = 2;
   // if a > b, then value in 'a' assigned to 'c'
   // else value in 'b' is assigned to 'a'                  
   int c = ( a > b ) ? a : b; // c holds greater of a and b
   cout << " Greater of " << a << " and " << b << " is " << c << endl;
   return 0;
}

Operator Precedence & Associativity
Precedence of operators determines the order of evaluation of expression consisting of multiple operators.
Higher precedence operators will be evaluated first. Associativity determines whether an expression is evaluated left to right or right to left. Consider the expressions below :

x = 2 + 3 * 2 => x = 2 + 6 => x = 8 ( '*' has higher precedence than '+' )
y = 2 * 3 / 2 => y = 6 / 2 => y = 3 ( '*' and '/' has same precedence but associativity is left to right )
z = 3 / 2 * 2 => z = 1 * 2 => z = 2 ( '*' and '/' has same precedence but associativity is left to right )

Following table shows the precedence and associativity of the C++ operators ( Operator precedence decreases as we move downwards ) :

OperatorsPrecedence
Postfix ( ++, -- )Left to Right
Unary ( +, -, !, ~, sizeof ), Prefix ( ++, -- )Right to Left
*, /, %Left to Right
Additive ( +, - )Left to Right
Left and Right Shift ( <<, >> )Left to Right
Relational ( <, <=, >, >= )Left to Right
Equality ( ==, != )Left to Right
Bitwise AND ( & )Left to Right
Bitwise XOR ( ^ )Left to Right
Bitwise OR ( | )Left to Right
Logical AND ( && )Left to Right
Logical OR ( || )Left to Right
Conditional ( Condition ? x : y )Right to Left
Assignment ( = )Right to Left

*Note : Unary operators have single operand

Postfix and Prefix Operators
The increment (++) and decrement (--) operators can appear either on left side (prefix) or right side (postfix) of the operand. Following program demonstrates the behaviour of postfix and prefix operators :

/*
 * Demonstration of postfix and prefix operators
 */

#include<iostream>
using namespace std;

int main() {
   int a = 5, b = 2;
   int c = 7, d = 3;
   int x, y, r, s;
   x = ++a; // first increment a, then assign its value to x
   cout << " x : " << x << ", a : " << a << endl; // displays x : 6, a : 6
   y = b++; // first assign value of b to y, then increment b
   cout << " y : " << y << ", b : " << b << endl; // displays y : 2, b : 3
   r = --c; // first decrement c, then assign its value to r
   s = d--; // first assign value of d to s, then decrement d
   cout << " r : " << r << ", c : " << c << endl; // displays r : 6, c : 6
   cout << " s : " << s << ", d : " << d << endl; // displays s : 3, d : 2    
   return 0;
}

Back | Next