C++ Operators
Operators in C++ is that An Operator performs operations on data value stored in variable, constants and array etc. C Operators take one or more operand as input and give a result after applying operation over them. Like in “a+b”, ‘a’ and ‘b’ are the operands and ‘+’ is an operator which performs the add operation on ‘a’ and ‘b’ operands. Types of operators in C++are Arithmetic Operators, Relational Operators, Logical Operators, Assignment Operators, Bitwise Operators, and Conditional Operator.
Eitworld.com provides the simple learning on Operators in C++without any cost in the easy and better way.
 An Operator performs operations on data value stored in variable, constants and array etc.
 Operators take one or more operand as input and give a result after applying operation over them. Like in “a+b”, ‘a’ and ‘b’ are the operands and ‘+’ is an operator which performs the add operation on ‘a’ and ‘b’ operands.
 C++ provides rich set of operators which are as follows:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Assignment Operators
 Bitwise Operators
 Conditional Operator
 Let us discuss the C++ operators in detail:
Arithmetic Operators


 Arithmetic operators are the operators which perform the mathematical operation between two operands or it can be more than two operands.
 We can classified the Arithmetic Operators in two categories:
i.Binary Operators


 ii.Unary Operators
a)Binary Operators

 Binary Operators are those arithmetic operators which take two or more operands to perform operation.
 Here is the list of Binary operators supported by the C++ language:
b)Unary Operators

 Unary Operators are those operator which takes only one operand to perform operation.
 Here is the list of the Unary Arithmetic operators supported by the C++ language:

 Here is a simple example which shows the use of Arithmetic operators in C++ language as follows:
Example
#include<iostream.h>
#include<conio.h>
int main()
{
int a=4,b=2;
clrscr();
cout<<“nAddition:”<<(a+b);
cout<<“nSubtraction:”<<(ab);
cout<<“nMultiplication:”<<(a*b);
cout<<“nDivison:”<<(a/b);
cout<<“nModular Division:”<<(a%b);
cout<<“nIncrement of a:”<<(++a);
cout<<“nDecrement of b:”<<(–b);
getch();
return 0;
}
Output
Addition: 6
Subtraction: 2
Multiplication: 8
Division: 2
Modular Division: 0
Increment of a:5
Decrement of b:1
Relational Operators

 Relational operators are used for the comparison between two values.
 Here is table containing the list of several relational operators:

 Here is an example of using relational operator in C++ language:
Example
#include<iostream.h>
#include<conio.h>
int main()
{
cout<<“n4>3:”<<(4>3);
cout<<“n4<3:”<<(4<3);
cout<<“n4<=4:”<<(4<=4);
cout<<“n2>=3:”<<(2>=3);
cout<<“n2==2:”<<(2==2);
cout<<“n2!=2:”<<(2!=2);
getch();
return 0;
}

 The output of the above program is as follows:
Output
4>3:1
4<3:0
4<=4:1
2>=3:0
2==2:1
2!=2:0
Logical Operators

 Logical operators check the logical relationship between the two expressions or expressions can be more than two.
 Here is the table containing the list of several Logical Operators :

 Logical AND (&&) operator will return true value (1) if both the expressions are true otherwise it will return false value (0).
 Logical OR () operator will return true value (1) when at least one of the expression is true otherwise it will return false value (0).
 Logical NOT (!) operator will return true value(1) when condition is false and if condition is true it will return false value (0). This operator inverts the result. It is also called as invert operator.
 Here is an example of using logical operators in C++ language:
Example
#include<iostream.h>
#include<conio.h>
int main()
{
cout<<“n2>1 && 3>2:”<<(2>1&&3>2);
cout<<“n2<12>3:”<<(2<12>3);
cout<<“n2!=2:”<<(2!=2);
getch();
return 0;
}

 The output of the above program is as follows:
Output
2>1 && 3>2:1
2<1  2>3:0
2 !=2:0
Assignment Operators

 Assignment operators are used to assign the value in variables, arrays or constant. It is denoted by = symbol.
 Here is a list of Assignment operators in the given table:

 Here is an example of using Assignment Operators in C++ language:
Example
#include<iostream.h>
#include<conio.h>
int main()
{
int x = 10;
int y = 5;
x = y;/*simple assignment*/
cout<<“nResult of x=y:”<<x;
x += y;/*addition and assignment*/
cout<<“nResult of x+=y:”<<x;
x = y;/*subtraction and assignment*/
cout<<“nResult of x=y:”<<x;
x*=y;/*multiplication and assignment*/
cout<<“nResult of x*=y:”<<x;
x /= y;/*division and assignment*/
cout<<“nResult of x/=y:”<<x;
x %= y;/*modulus and assignment*/
cout<<“nResult of x%=y:”<<x;
getch();
return 0;
}

 Here is the output of the above program:
Output
Result of x=y:x=5
Result of x+=y:x=10
Result of x=y:x=5
Result of x*=y:x=25
Result of x/=y:x=5
Result of x%=y:x=0
Bitwise Operators

 Bitwise operators are used to perform operation on binary representation of numeric values.
 Here is the list of Bitwise operators in the given table:


 Let us discuss the operation performed by Bitwise operators on the value one by one:
1) Right Shift Operator (>>)


 Let us take a value a=8 then what would be the result of a>>2? We have a value a=8 and binaryRepresentation of numeric value 8 is as follows:
0000000000001000

 The value of variable a is shifted by 2 bit right side so the result would be as follows:
0000000000000010
 It is the binary representation of the numeric value 2 so the result of a>>2 would be=22)Left Shift Operator (<<)
 Let us take a value a=8 then what would be the result of a<<2? We have a value a=8 and binary Representation of numeric value 8 is as follows:

0000000000001000
 The value of variable a is shifted by 2 bit left side so the result would be as follows:

0000000000100000
 It is the binary representation of the numeric value 32 so the result of a>>2 would be=32
 Before discussing about other bitwise operators let us see their truth tables:
 Here is a truth table for ^, &,  operators:
 Truth table of ~ operator is as follows:
 3)Bitwise XOR operator (^)
 Let us take two values a=8 and b=4 then what would be the result of a^b?
 Binary representation of a=8 is as follows:

0000000000001000
 Binary representation of b=4 is as follows:

0000000000000100
 Now a^b will give the result as follow:

0000000000001100
 It is the binary representation of the numeric value 12 so the result of a^b would be=12
 4)Bitwise OR operator (I)
 Let us take two values a=8 and b=4 then what would be the result of ab?
 Binary representation of a=8 is as follows:

0000000000001000
 Binary representation of b=4 is as follows:

0000000000000100
 Now ab will give the result as follow:

0000000000001100
 It is the binary representation of the numeric value 12 so the result of ab would be=12
 5)Bitwise AND operator (&)
 Let us take two values a=8 and b=4 then what would be the result of a&b?
 Binary representation of a=8 is as follows:

0000000000001000
 Binary representation of b=4 is as follows:

0000000000000100
 Now a&b will give the result as follow:

0000000000000000
 It is the binary representation of the numeric value 0 so the result of a&b would be=0.
 6)Bitwise One’s Complement operator (~)
 Let us take a value c=60 then what would be the result of ~c?
 Binary representation of c=60 is as follows:

00111100
 Now ~a will give the result as follow:

11000011
 It is the binary representation of the numeric value 61 so the result of ~c would be=61.
 Here is an example which of using bitwise operators in C++ language:

Example
#include<conio.h>
#include<iostream.h>
int main()
{
int a = 8;
int b= 4;
int c=60;
cout<<“right shift the value of a by bit 2=”<<(a>>2);
cout<<“nleft shift the value of a by bit 2=”<<(a<<2);
cout<<“nbitwise xor operation between a and b=”<<(a^b);
cout<<“nbitwise or operation between a and b=”<<(ab);
cout<<“nbitwise and operation between a and b=”<<(a&b);
cout<<“nbitwise one’s complement operation of c=”<<(~c);
getch();
return 0;
}  The output of the above program would be as follows:

Output
left shift the value of a by bit 2= 32
right shift the value of a by bit 2= 2
bitwise xor operation between a and b= 12
bitwise or operation between a and b= 12
bitwise and operation between a and b= 0
bitwise one’s complement operation of c= 61
Conditional Operator

 Conditional operators contain a condition followed by two statements or values. If the condition is true the first statement will be executed otherwise the second statement will be executed.
 The syntax of the conditional operator is given below:
Condition? (expression1): (expression2);

 If the condition is true expression1 gets evaluated and if condition is false then expression 2 gets evaluated.
 Here is an example of using conditional operator in C++ program:
Example
#include<iostream.h>
#include<conio.h>
int main()
{
cout<<“Output=”<<(2>3?1:0);
return 0;
}

 The output of the above program would be as follows:
Output
Output=0