These are the tokens which are used to do some specific task, condition testing, calculation c++ is rich in operator. An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations.
C++ is rich in built-in operators and provide the following types of operators:
** The value on which the operator operates is called an
operand.
Type of Operators
1. Arithmetic Operator
2. Increment/Decrement Operator
3. Relational Operator
4. Conditional /Ternary Operator
5. Logical/Short circuit Operator
6. Bitwise Operator
7. Ones Complement [~]
8. Sizeof Operator
9. Comma Operator
10. Assignment and Shorthand Operator
1. Arithmetic Operator
It is used to do mathematic calculation like: + , - , * , / , % etc.
Operator |
Description |
Example |
+ |
Adds two operands |
A + B will give 6 |
- |
Subtracts
second operand from the first |
A - B will give 2 |
* |
Multiplies both operands |
A * B will give 8 |
/ |
Divides
numerator by denumerator |
B / A will give 2 |
% |
Modulus
Operator and remainder of
after an integer division |
B % A will give 0 |
Example if a=4 and b=2
a+b
= 6
a-b
= 2
a*b
= 8
a/b
= 2
a%b = 0
Example of Arithmetic Operator
#include<iostream.h>
#include<conio.h>
void main(){
int i=5,j=2,k;
clrscr();
k=i+j;
cout<<"i+j="<<k <<endl;
k=i-j;
cout<<"i-j="<<k <<endl;
k=i*j;
cout<<"i*j="<<k <<endl;
k=i/j;
cout<<"i/j="<<k <<endl;
float k=i/j;
cout<<"i/j="<<k <<endl;
k=i%j;
cout<<"i//j="<<k <<endl;
getch(); }
Output
i + j = 7
i - j = 3
i * j = 10
i / j = 2
i / j = 2.0
i % j = 1
2. Increment/Decrement Operator
Increment operator [++] adds one to each value
Decrement operator [- -] subtract one to each value. It is of two types
1.
Prefix ++ , --
2.
Postfix ++ , --
Prefix -> It first add the value and then operates
Postfix -> It first operates and then add value
#include<stdio.h>
#include<conio.h>
void main(){
int i=2;
clrscr();
i=i++ + ++i + ++i + i++;
cout<<i++ <<endl <<++i <<endl <<++i;
getch();
}
Output
20
20
19
3. Relational Operator
It is also known as comparison operator it determine
the relation between the value its answer come in true or false. (< > ,
<= , >= , == , !=)
Example if a=10 and b=7
a>b
= false
a>=b
= false
a<b
= true
a<=b
= true
a==b
= false
a!=b
= true
Example of Relational Operator
#include<iostream.h>
#include<conio.h>
void main(){
int i=2,j=3;
clrscr();
i=i++<++j<++i;
cout<<i++ <<++j <<++i;
getch();
}
Output
3
5
3
4. Conditional/Ternary Operator
It stores value depends upon a condition It is also
known as ternary operator syntax:-
Condition? True:
False
Example of Ternary/Conditional Operator
#include<iostream.h>
#include<conio.h>
void main(){
int i,j,k,l,max;
clrscr();
cout<<"Enter value of i=";
cin>>i;
cout<<"Enter value of j=";
cin>>j;
cout<<"Enter value of k=";
cin>>k;
cout<<"Enter value of l=";
cin>>l;
max=i>j?(i>k?(i>l?i:l):(k>l?k:l)):(j>k?(j>l?j:l):(k>l?k:l));
cout<<"max=" <<max;
getch();
}
Output
Enter value of i=9
Enter value of j=19
Enter value of k=56
Enter value of l=34
Max=56
5. Logical/Short circuit Operator
It is used to combine the existing expression .It is
also used to examine the arithmetic and relational operator. It is of 3 type
5.1. Logical AND [&&]
When both the condition are true then it works
Experation 1
&& Experation 2
False -> no
Post increment/decrement in exp1 and store 0(zero)
left side variable.
Experation 1
&& Experation 2
True -> yes
Post increment/decrement in exp1 and check exp2
If exp2 false
Post increment/decrement in exp2
Store 0(zero) into left side variable
If exp2 true
Post increment/decrement in exp2
Store 1(one) into left side variable
A |
B |
X=A.B |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
Example of Logical AND [&&]
#include<iostream.h>
#include<conio.h>
void main(){
int i=3,j=0,k=2,l=5;
clrscr();
l=cout<<i=7 && <<j=0 && cout<<k=20 && cout<<i;
printf("%d%d%d%d",i,j,k,l;
getch();
}
Output
7 7 0 2 0
5.2. Logical OR [||]
When any one condition is true then it operates
A |
B |
X=A+B |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
#include<iostream.h>
#include<conio.h>
void main(){
int i=1;
clrscr();
i=i++||++i||++i;
cout<<i++,++i,++i;
getch();
}
Output
3 3 2
5.3. Logical NOT [!]
A |
X=A |
1 |
0 |
0 |
1 |
#include<stdio.h>
#include<conio.h>
void main(){
int i=5,j=3,k;
clrscr();
k=!cout<<!i < cout<<!j < !cout<<"";
cout<<k;
getch();
}
Output
0 0 0
6. Bitwise Operator
Bitwise
operator works on bits and perform bit-by-bit operation. The truth tables
for
& , | , ^ , >> , << , ~ are as follows:
P |
Q |
P & Q |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
6.1. Bitwise AND [&]
Binary AND
Operator copies a bit to the result
if it
exists in both operands.
Example -
(A & B) will give 12 which is 0000 1100
Example of Bitwise AND [&]
#include<iostream.h>
#include<conio.h>
void main(){
int i=5,j=7,k;
clrscr();
k=i&j;
cout<<k;
getch();
}
Output
5
6.2. Bitwise OR [|]
Binary OR
Operator copies a bit if it exists in either operand.
Example -
(A | B) will give 61 which is 0011 1101
P |
Q |
P | Q |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
#include<iostream.h>
#include<conio.h>
void main(){
int i=7,j=11,k=2;
clrscr();
k=i++|++j|++i|k++;
cout<<i,j,k;
getch();
}
Output
9 12 15
6.3. Bitwise XOR [^]
Binary XOR
Operator copies the bit if it is set in one
operand
but not both.
P |
Q |
P | Q |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
#include<iostream.h>
#include<conio.h>
void main(){
int i=3,j=7,k;
clrscr();
k=i^j;
cout<<k;
getch();
}
Output
4
6.4. Bitwise Right Sift [>>]
Binary
Right Shift Operator. The left operands value is moved right by the number of
bits
specified
by the right operand.
#include<iostream.h>
#include<conio.h>
void main(){
int i=23,j;
clrscr();
j=i>>2;
cout<<i <<endl <<j;
getch();
}
Output
23
5
6.5. Bitwise Lift Sift [<<]
Binary
Left Shift Operator. The left operands value is moved left by the number of
bits specified by the right operand.
Example -
A << 2 will give 240 which is 1111 0000
#include<iostream.h>
#include<conio.h>
void main(){
int i=27,j=2;
clrscr();
j=i++>>j++<<++j;
cout<<i <<endl <<j;
getch();
}
Output
28
25
7. Ones Complement [~]
Binary
Ones Complement Operator is unary and has the effect of 'flipping' bits.
Example -
(~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed
binary number.
#include<iostream.h>
#include<conio.h>
void main(){
int i=1,j;
clrscr();
j=~i;
cout<<j;
getch();
}
Output
-2
8. Sizeof Operator
Sizeof
operator returns the size of a variable.
For example, sizeof(a), where ‘a’ is integer, and will return 4.
Sizeof (Data_Type)
Sizeof (Variable)
Sizeof ( Value)
#include<iostream.h>
#include<conio.h>
void main(){
int i=5;
clrscr();
cout<<sizeof(i) <<endl;
cout<<sizeof(int) <<endl;
cout<<sizeof(10) <<endl;
cout<<sizeof(3.45) <<endl;
getch();
}
Output
2 2 2 8
9. Comma
Operator [,]
Comma
operator causes a sequence of operations to be performed. The value of the
entire comma expression is the value of the last expression of the comma-separated
list.
Example - (x=18,y=2,=20)
#include<iostream.h>
#include<conio.h>
void main(){
int i=5,j=7,k=6,l;
clrscr();
l=i,j,k;
cout<<i,j,k,l;
getch();
}
Output
5 7 6 5
10. Assignment and Shorthand Operator
This operator is used to store a value in variable of
any variable. In left part is always used to variable.
#include<iostream.h>
#include<conio.h>
void main(){
int i=4,j=11,k=6,l=7;
clrscr();
l=i++ + j++ * ++i / l++ + ++j % ++i;
cout<<i,j,k,l;
getch();
}
Output
7 13 6 17
Operator |
Description |
Example |
= |
Simple assignment operator, Assigns values
from right side operands to left side operand. |
C = A + B will assign value of A + B into C |
+= |
Add AND assignment operator, It adds right
operand to the left operand and assign the result to left operand. |
C += A is equivalent to C = C + A |
-= |
Subtract AND assignment operator, It
subtracts right operand from the left operand and assign the
result to left operand. |
C -= A is equivalent to C = C - A |
*= |
Multiply AND assignment operator, It
multiplies right operand with the left operand and assign the
result to left operand. |
C *= A is equivalent to C = C * A |
/= |
Divide AND assignment operator, It divides
left operand with the right operand and assign the result to left operand. |
C /= A is equivalent to C = C / A |
%= |
Modulus AND assignment operator, It takes
modulus using two operands and assign the result to
left operand. |
C %= A is equivalent to C = C % A |
<<= |
Left shift AND assignment operator.
|
C <<= 2 is same as C = C << 2 |
>>= |
Right shift AND assignment operator.
|
C >>= 2 is same as C = C >> 2 |
&= |
Bitwise AND assignment operator.
|
C &= 2 is same as C = C & 2 |
^= |
Bitwise exclusive OR and assignment
operator.
|
C ^= 2 is same as C = C ^ 2 |
|= |
Bitwise inclusive OR and assignment
operator.
|
C |= 2 is same as C = C | 2 |
0 Comments