Description
CPP Operators are symbols which tells the compiler to perform either mathematical or logical operation on the variables or a value. C++ language supports rich set of built-in operators.
Description
Assignment CPP Operators assigns values
to variables.Below is the list of Assignment Operators.
Operators
Operator |
Description |
Example |
= |
Assignment Operator: Assigns right-hand side(called rvalue)to left-hand side(called lvalue) |
z=x+y assigns value of x+y into z |
+= |
Add AND Assignment Operator: Adds right operand to left operand and assigns result to left operand |
y+=x is equivalent to x=y+x |
-= |
Subtract AND Assignment Operator: Subtracts right operand from left operand and assigns result to left operand |
y-=x is equivalent to y=y-x |
*= |
Multiply AND Assignment Operator: Multiplies right operand from left operand and assigns result to left operand |
y*=x is equivalent to x=y*x |
/= |
Divide AND Assignment Operator: Divides right operand from left operand and assigns result to left operand |
y/=x is equivalent to x=y/x |
%= |
Modulus AND Assignment Operator: Takes modulus using two operands and assigns result to left operand |
y%=x is equivalent to x=y%x |
<<= |
Left shift AND Assignment Operator |
y<<=2 is same as y=y<<2 |
>>= |
Right shift AND Assignment Operator |
y>>=2 is same as y=y>>2 |
&= |
Bitwise AND Assignment Operator |
y&=2 is same as y=y&2 |
^= |
Bitwise Exclusive OR and Assignment Operator |
y^=2 is same as y=^2 |
|= |
Bitwise Inclusive OR and Assignment Operator |
y|=2 is same as y=y|2 |
Example
[c]
#include <iostream>
using namespace std;
int main ()
{
int x,y;
x=5;
y=7;
x=y;
y=9;
cout<< "x="<<x<<endl;
cout<< "y="<< y<<endl;
}[/c]
Output:
[c]
x=7
y=9[/c]
Description
Arithmetic operators performs mathematical calculations
like addition, subtraction, multiplication, division, and modulus. Suppose x=10 and y=20.
Operators
Operator |
Description |
Example |
+ |
Adds two operands |
x+y gives 30 |
- |
Subtracts second operand from the first |
x-y gives -10</td |
* |
Multiplies operands |
x*y gives 200 |
/ |
Divide numerator by denominator |
x/y gives 2 |
% |
Modulus operator gives remainder after an integer division |
y%x gives 0 |
++ |
Increment operator: Increases integer value by one |
x++ gives 11 |
-- |
Decrement operator: Decreases integer value by one |
x-- gives 9 |
Example
[c]
#include <iostream>
using namespace std;
int main()
{
int a = 50;
int b = 25;
int c ;
c = a + b;
cout <<"Addition of a and b:"<<c << endl ;
c = a - b;
cout << "Subtraction of a and b:"<< c << endl ;
c = a * b;
cout << "multiplication of a and b "<< c << endl ;
c = a / b;
cout<< "division of a and b " << c<<endl ;
c = a % b;
cout<< "modular division of a and b "<< c &<< endl ;
c = a++;
cout << "Incrementing a gives " <<c << endl ;
c = a--;
cout << "Decrementing a gives " << c << endl ;
return 0;
}[/c]
Output:
[c]
Addition of a and b:75
Subtraction of a and b:25
multiplication of a and b 1250
division of a and b 2
modular division of a and b 0
Incrementing a gives 50
Decrementing a gives 51
[/c]
Description
Relational operators can be used to compare
two variables. Below is the list of Relational Operators in C++.
Operators
Operator |
Description |
== |
Checks if the value is equal or not, if yes then returns true |
!= |
Checks if the value is equal or not, if values are not equal then condition becomes true |
> |
Checks for greater value |
< |
Checks for lower value |
>= |
Checks if the value is greater than or equal to the value |
<= |
Checks if the value is less than or equal to the value |
Example
[c]
#include <iostream>
using namespace std;
int main()
{
int x= 50;
int y = 10;
int z ;
if( x == y )
{
cout << " x is equal to y" << endl ;
}
else
{
cout << "x is not equal to y" << endl ;
}
if ( a < b )
{
cout << " x is less than y" << endl ;
}
else
{
cout << "x is not less than y" << endl ; } if ( x > y )
{
cout << "x is greater than y" << endl ;
}
else
{
cout << "x is not greater than y" << endl ;
}
if ( x<= y )
{
cout << "x is either less than or euqal to y" << endl ; } if ( y>= x)
{
cout << "y is either greater than or equal to x" << endl ;
}
return 0;
}[/c]
Output:
[c]
x is not equal to y
x is not less than y
x is greater than y[/c]
Description
Bitwise operators are used to convert values
into binary digits. Shift operator also comes under Bitwise operator because the shift is done in terms of bits.
Shift Operator: It is used to shift the values to either left or right.
(i)Left -Shift Operator: It moves the bits to the left by adding zeros at the right end. It can be represented by "<<"
(ii)Right-Shift Operator: It moves the bits to right by adding zeros at the left end. Here, if the value is unsigned it fills all the bit memory with zeros until the left most bit. If the value is signed, operator assigns one bit for sign and fills other bits with zeros. It is represented by ">>"
Operators
Operator |
Symbol |
Form |
Operation |
Right shift |
>> |
x>>y |
all bits in x shifted right y bits |
Left shift |
<< |
x<<y |
all bits in x shifted left y bits |
Bitwise AND |
& |
x&y |
each bit in x AND each bit in y |
Bitwise OR |
| |
x|y |
each bit in x OR each bit in y |
Bitwise XOR |
^ |
x^y |
each bit in x XOR each bit in y |
Bitwise NOT |
~ |
~x |
all bits in x flipped |
Example
[c]
#include<iostream>
using namespace std;
int main()
{
int x = 10; // x=1010
int y = 7; // y=0111
cout << "x & y = " << (x&y) << "\n";
cout << "x | y = " << (x|y) << "\n";
cout << "x ^ y = " << (x^y) << "\n";
cout << "~x = " << (~x) << "\n";
cout << "~y = " << (~y) << "\n";
cout << "x >> y = " << (x>>y) << "\n";
cout << "x << y = " << (x<<y) << "\n";
}[/c]
Output:
[c]x & y = 2
x | y = 15
x ^ y = 13
~x = -11
~y = -8
x >> y = 0
x << y = 1280[/c]
Description
Logical operators performs logical operations
on given expressions. Below is the list of logical operators.
Operators
Operator |
Name of Operator |
Output |
&& |
AND Operator |
Output is 1 only when conditions on both sides of operator becomes true |
|| |
OR Opeator |
Output is 0 only when conditions on both sides of operator becomes false |
! |
NOT Operator |
Gives inverted output |
Example
[c]
#include<iostream>
using namespace std;
int main()
{
int a=30;
int b=20;
if(a>=30 || b>=40)
cout<<"OR Block is executed"<<endl; if(a>=20 && b>=20)
cout<<"AND Block is executed"<<endl; if(!(a>=50))
cout<<"NOT Block is executed"<<endl;
return 0;
}[/c]
Output:
[c]
OR Block is executed
AND Block is executed
NOT Block is executed[/c]
Description
Some other operators in C++ are given below.
Operators
Operator |
Name |
Description |
sizeof |
Size of Operator |
Returns the size of a variable |
Condition ? X : Y |
Conditional Operator |
Checks the condition. If the condition is true, then it returns value X, otherwise value Y</td |
& |
Pointer Operator |
Returns the address of a variable |
* |
Pointer Operator |
Points to a variable |
, |
Comma Operator |
Makes sequence of operations |
.(dot) and ->(arrow) |
Member Operator |
Used to refer "Classes, Structures and Unions" |
Cast |
Casting Operator |
Convert one data type to another |
Example
[c]
#include <iostream>
using namespace std;
int main ()
{
int x, y = 10,z=20,a,b, c=10.5;
x = (y < 10) ? 30 : 40;
a=sizeof(z);
b = (int) c;
cout << "value of x: " << x << endl;
cout << "size of y is " << a << endl;
cout << "casting of c variable gives " << b << endl;
return 0;
}[/c]
Output:
[c]
value of x: 40
size of y is 4
casting of c variable gives 10[/c]
Key Points
- CPP Operators performs "Mathematical and Logic Operations"
- "Arithmetic, Assignment, Relational, Increment and Decrement, Logical, Bitwise, Conditional" are few types of CPP Operators.
Programming
Tips
- Other than variable identifier, do not use increment and decrement CPP Operators with any expression
- Do not use variable without assigning any value to it
- Do not confuse between equality operator == with assignment operator =