C++ Operators
Operators are
symbols that perform operations on variables and values.
Two types:-
I.
According
to operands
II.
According
to functions
Operators in C++
can be classified into 6 types:
1.
Arithmetic
Operators
2.
Assignment
Operators
3.
Relational
Operators
4.
Logical
Operators
5.
Bitwise
Operators
6.
Other
Operators
1. Arithmetic
Operators
Arithmetic
operators are used to perform arithmetic operations on variables and data.
Types of Arithmetic operators
|
Operator |
Operation |
|
+ |
Addition |
|
- |
Subtraction |
|
* |
Multiplication |
|
/ |
Division |
|
% |
Modulo Operation
(Remainder after division) |
Example 1: Arithmetic
Operators
#include
<iostream>
using namespace
std;
int main()
{
int a, b;
a = 7;
b = 2;
// printing the sum of a and b
cout << "a + b = " <<
(a + b) << endl;
// printing the difference of a and b
cout << "a - b = " <<
(a - b) << endl;
// printing the product of a and b
cout << "a * b = " <<
(a * b) << endl;
// printing the division of a by b
cout << "a / b = " <<
(a / b) << endl;
// printing the modulo of a by b
cout << "a % b = " <<
(a % b) << endl;
return 0;
}
Output
a + b = 9
a - b = 5
a * b = 14
a / b = 3
a % b = 1
Increment and
Decrement Operators
C++ also provides
increment and decrement operators: ++ and --, respectively.
++ increases
the value of the operand by 1
-- decreases
it by 1
Example: Increment
and Decrement Operators
// Working of
increment and decrement operators
#include
<iostream>
using namespace
std;
int main() {
int a = 10, b = 100, result_a, result_b;
// incrementing a by 1 and storing the
result in result_a
result_a = ++a;
cout << "result_a = "
<< result_a << endl;
// decrementing b by 1 and storing the
result in result_b
result_b = --b;
cout << "result_b = "
<< result_b << endl;
return 0;
}
Output
result_a = 11
result_b = 99
2. Assignment
Operators
|
Operator |
Example |
Equivalent to |
|
= |
a = b; |
a = b; |
|
+= |
a += b; |
a = a + b; |
|
-= |
a -= b; |
a = a - b; |
|
*= |
a *= b; |
a = a * b; |
|
/= |
a /= b; |
a = a / b; |
|
%= |
a %= b; |
a = a % b; |
Example 3:
Assignment Operators
#include
<iostream>
using namespace
std;
int main() {
int a, b;
// 2 is assigned to a
a = 2;
// 7 is assigned to b
b = 7;
cout << "a = " << a
<< endl;
cout << "b = " << b
<< endl;
cout << "\nAfter a += b;"
<< endl;
// assigning the sum of a and b to a
a += b;
// a = a +b
cout << "a = " << a
<< endl;
return 0;
}
Output
a = 2
b = 7
After a += b;
a = 9
3. Relational
Operators
A relational
operator is used to check the relationship between two operands.
If the relation
is true, it returns 1, whereas if the relation is false, it
returns 0.
|
Operator |
Meaning |
Example |
|
== |
Is Equal To |
3 == 5 gives us
false |
|
!= |
Not Equal To |
3 != 5 gives us
true |
|
> |
Greater Than |
3 > 5 gives
us false |
|
< |
Less Than |
3 < 5 gives
us true |
|
>= |
Greater Than or
Equal To |
3 >= 5 give
us false |
|
<= |
Less Than or
Equal To |
3 <= 5 gives
us true |
Example 4:
Relational Operators
#include
<iostream>
using namespace
std;
int main() {
int a, b;
a = 3;
b = 5;
bool result;
result = (a == b); // false
cout << "3 == 5 is "
<< result << endl;
result = (a != b); // true
cout << "3 != 5 is "
<< result << endl;
result = a > b; // false
cout << "3 > 5 is "
<< result << endl;
result = a < b; // true
cout << "3 < 5 is "
<< result << endl;
result = a >= b; // false
cout << "3 >= 5 is "
<< result << endl;
result = a <= b; // true
cout << "3 <= 5 is "
<< result << endl;
return 0;
}
Output
3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1
4. Logical
Operators
Logical operators
are used to check whether an expression is true or false. If the
expression is true, it returns 1, whereas if the expression
is false, it returns 0.
|
Operator |
Example |
Meaning |
|
&& |
expression1
&& expression2 |
Logical AND. |
|
|| |
expression1 ||
expression2 |
Logical OR. |
|
! |
!expression |
Logical NOT. |
Logical operators
are commonly used in decision-making.
Example: Logical
Operators
#include
<iostream>
using namespace
std;
int main() {
bool result;
result = (3 != 5) && (3 <
5); // true
cout << "(3 != 5) && (3
< 5) is " << result << endl;
result = (3 == 5) && (3 <
5); // false
cout << "(3 == 5) && (3
< 5) is " << result << endl;
result = (3 == 5) && (3 >
5); // false
cout << "(3 == 5) && (3
> 5) is " << result << endl;
result = (3 != 5) || (3 < 5); // true
cout << "(3 != 5) || (3 < 5)
is " << result << endl;
result = (3 != 5) || (3 > 5); // true
cout << "(3 != 5) || (3 > 5)
is " << result << endl;
result = (3 == 5) || (3 > 5); // false
cout << "(3 == 5) || (3 > 5)
is " << result << endl;
result = !(5 == 2); // true
cout << "!(5 == 2) is "
<< result << endl;
result = !(5 == 5); // false
cout << "!(5 == 5) is "
<< result << endl;
return 0;
}
Output
(3 != 5)
&& (3 < 5) is 1
(3 == 5)
&& (3 < 5) is 0
(3 == 5)
&& (3 > 5) is 0
(3 != 5) || (3
< 5) is 1
(3 != 5) || (3
> 5) is 1
(3 == 5) || (3
> 5) is 0
!(5 == 2) is 1
!(5 == 5) is 0
5. Bitwise
Operators
Bitwise operators
perform operations on integer data at the individual bit level.
These operations
include testing, setting, or shifting the actual bits.
Concept of bit:-
A bit (Binary
digit) is the basic unit of information which stored in two states, as ON or
OFF. The
ON state is represented by 1, and the OFF state is represented by 0.
Types of bitwise opt.
|
Operator |
Description |
|
& |
Bitwise AND
Operator |
|
| |
Bitwise OR
Operator |
|
^ |
Bitwise XOR
Operator |
|
~ |
Bitwise
Complement Operator |
|
<< |
Bitwise Shift
Left Operator |
|
>> |
Bitwise Shift
Right Operator |
1. Bitwise AND
Operator
The bitwise
AND & operator returns 1 if and only if both the
operands are 1. Otherwise, it returns 0.
|
a |
b |
a & b |
|
0 |
0 |
0 |
|
0 |
1 |
0 |
|
1 |
0 |
0 |
|
1 |
1 |
1 |
Note: The
table above is known as the "Truth Table" for the bitwise
AND operator.
Example:-
x=13; y=22; echo x
& y;
Let's take a look
at the bitwise AND operation of two integers 12 and 25:
12 = 00001100 (In
Binary)
25 = 00011001 (In
Binary)
//Bitwise AND
Operation of 12 and 25
00001100
& 00011001
_________
00001000
= 8 (In decimal)
Example 1: Bitwise AND
#include
<iostream>
using namespace
std;
int main() {
// declare variables
int a = 12, b = 25;
cout << "a = " << a
<< endl;
cout << "b = " << b
<< endl;
cout << "a & b = "
<< (a & b) << endl;
return 0;
}
Output
a = 12
b = 25
a & b = 8
2. C++ Bitwise OR
Operator
The bitwise
OR | operator returns 1 if at least one of the operands
is 1. Otherwise, it returns 0.
The following
truth table demonstrates the working of the bitwise OR operator.
|
a |
b |
a | b |
|
0 |
0 |
0 |
|
0 |
1 |
1 |
|
1 |
0 |
1 |
|
1 |
1 |
1 |
Let us look at
the bitwise OR operation of two integers 12 and 25:
12 = 00001100 (In
Binary)
25 = 00011001 (In
Binary)
Bitwise OR
Operation of 12 and 25
00001100
| 00011001
_________
00011101
= 29 (In decimal)
Example 2: Bitwise
OR
#include
<iostream>
int main() {
int a = 12, b = 25;
cout << "a = " << a
<< endl;
cout << "b = " << b
<< endl;
cout << "a | b = " <<
(a | b) << endl;
return 0;
}
Output
a = 12
b = 25
a | b = 29
The bitwise
OR of a = 12 and b = 25 gives 29.
3. C++ Bitwise XOR
Operator
The bitwise
XOR ^ operator returns 1 if and only if one of the operands
is 1. However, if both the operands are 0, or if both are 1,
then the result is 0.
|
a |
b |
a ^ b |
|
0 |
0 |
0 |
|
0 |
1 |
1 |
|
1 |
0 |
1 |
|
1 |
1 |
0 |
Let us look at
the bitwise XOR operation of two integers 12 and 25:
12 = 00001100 (In
Binary)
25 = 00011001 (In
Binary)
Bitwise XOR
Operation of 12 and 25
00001100
^ 00011001
_________
00010101
= 21 (In decimal)
Example 3: Bitwise
XOR
#include
<iostream>
int main() {
int a = 12, b = 25;
cout << "a = " << a
<< endl;
cout << "b = " << b
<< endl;
cout << "a ^ b = " <<
(a ^ b) << endl;
return 0;
}
Output
a = 12
b = 25
a ^ b = 21
The bitwise
XOR of a = 12 and b = 25 gives 21.
4. C++ Bitwise
Complement Operator
The bitwise
complement operator is a unary operator (works on only one operand).
It is denoted
by ~ that changes binary
digits 1 to 0 and 0 to 1.
Bitwise Complement
// Using bitwise
complement operator
~ 00100011
__________
11011100
In the above
example, we get that the bitwise complement of 00100011 (35)
is 11011100. Here, if we convert the result into decimal we get 220.
2's Complement
The 2's complement
of a number N gives -N.
In binary
arithmetic, 1's complement changes 0 to 1 and 1 to 0.
And, if we
add 1 to the result of the 1's complement, we get the 2's complement
of the original number.
For example,
36 = 00100100 (In
Binary)
1's Complement = 11011011
2's Complement :
11011011
+ 1
_________
11011100
The bitwise
complement of 35 = -36.
Example 4: Bitwise
Complement
#include
<iostream>
int main() {
int num1 = 35;
int num2 = -150;
cout << "~(" << num1
<< ") = " << (~num1) << endl;
cout << "~(" << num2
<< ") = " << (~num2) << endl;
return 0;
}
Output
~(35) = -36
~(-150) = 149
The bitwise
complement of 35 = - (35 + 1) = -36
i.e. ~35 = -36
C++ Shift
Operators
There are two
shift operators in C++ programming:
Right shift
operator >>
Left shift
operator <<
5. C++ Right Shift
Operator
The right
shift operator shifts all bits towards the right by a certain number
of specified bits. It is denoted by >>.
When we shift any
number to the right, the least significant bits are discarded, while
the most significant bits are replaced by zeroes.
One bit Right
Shift
x=96; y=5;
print x >> y;
Output 3
6. C++ Left Shift
Operator
The left
shift operator shifts all bits towards the left by a certain number
of specified bits. It is denoted by <<.
One bit Left Shift
x=8; y=3;
print $x << $y;
Output 64
In the above
example, the value of x, which is 8 is taken, and a BIT SHIFT LEFT operation is
performed. So, 8 is multiplied by 2 thrice. Thus, we get 8 x 2 x 2 x 2 = 64.
Example 5: Shift
Operators
#include
<iostream>
int main() {
// declaring two integer variables
int num = 212, i;
// Shift Right Operation
cout << "Shift Right:"
<< endl;
// Using for loop for shifting num right
from 0 bit to 3 bits
for (i = 0; i < 4; i++) {
cout << "212 >> "
<< i << " = " << (212 >> i) << endl;
}
// Shift Left Operation
cout << "\nShift Left:"
<< endl;
// Using for loop for shifting num left
from 0 bit to 3 bits
for (i = 0; i < 4; i++) {
cout << "212 << "
<< i << " = " << (212 << i) << endl;
}
return 0;
}
Output
Shift Right:
212 >> 0 =
212
212 >> 1 =
106
212 >> 2 =
53
212 >> 3 =
26
Shift Left:
212 << 0 =
212
212 << 1 =
424
212 << 2 =
848
212 << 3 =
1696
6. Other C++
Operators
Here's a list of
some other common operators available in C++. We will learn about them in later
tutorials.
|
Operator |
Description |
Example |
|
sizeof |
Returns the size
of the data type |
sizeof(int); //
4 |
|
?: |
Returns a value
based on the condition |
string result =
(5 > 0) ? "even" : "odd"; // "even" |
|
& |
represents the memory address of the operand |
# //
address of num |
|
. |
accesses members
of struct variables or class objects |
s1.marks = 92; |
|
-> |
used with
pointers to access the class or struct variables |
ptr->marks =
92; |
|
<< |
prints the
output value |
cout << 5; |
|
>> |
gets the input
value |
cin >>
num; |
|
Scope Resolution
Operator (: :)
|
used to access
the global variable when both local and global variables have the same
name, to refer to the static members of a class, and to define a function
definition outside the class. |
|
==============================================================
Operators
Precedence in C++
|
Category |
Operator |
Associativity |
|
Postfix |
() [] -> . ++
- - |
Left to
right |
|
Unary |
+ - ! ~ ++ - -
(type)* & sizeof |
Right to
left |
|
Multiplicative
|
* / % |
Left to
right |
|
Additive |
+ - |
Left to
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 |
?: |
Right to
left |
|
Assignment |
= += -= *= /=
%=>>= <<= &= ^= |= |
Right to
left |
|
Comma |
, |
Left to
right |
========================================================================
0 Comments