Operators are symbols
that perform operations on variables and values. For example, +
is an operator used for
addition, while -
is an operator used for subtraction.
Operators in C++ can be classified into 6 types:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Other Operators
1. Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. For example,
a + b;
Here, the +
operator is used to add
two variables a
and b.
Similarly there are various other arithmetic operators in C++.
Operator |
Operation |
|
Addition |
|
Subtraction |
|
Multiplication |
|
Division |
|
Modulo Operation (Remainder after division) |
Example
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 8;
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 = 10
a - b = 6
a * b = 16
a / b = 4
a % b = 0
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
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 100, result1, result2;
// incrementing a by 1 and storing the result in result1
result1 = ++a;
cout << "result1 = " << result1 << endl;
// decrementing b by 1 and storing the result in result2
result2 = --b;
cout << "result2 = " << result2 << endl;
return 0;
}
Output
result1 = 11
result2 = 99
2. C++ Assignment Operators
In C++, assignment operators are used to assign values to variables. For example,
// assign 5 to a
a=5;
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; |
3. C++ Relational Operators
A relational operator is used to check the relationship between two operands. For example,
// checks if a is greater than b
a > b;
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 |
Note: Relational operators are used in decision-making and loops.
4. C++ 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. |
5. C++ Bitwise Operators
In C++, bitwise operators are used to perform operations on individual bits.
Operator |
Description |
& |
Binary AND |
| |
Binary OR |
^ |
Binary XOR |
~ |
Binary One's Complement |
<< |
Binary Shift Left |
>> |
Binary Shift Right |
The bitwise AND &
operator returns 1 if and only if
both the operands are 1.
Otherwise, it returns 0.
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)
The bitwise
OR |
operator returns 1 if at least one of the
operands is 1.
Otherwise, it returns 0.
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)
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.
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)
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.
6. 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.
7. 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 <<
.
Example
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
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 data type |
sizeof(int); // 4 |
?: |
returns value based on the condition |
string result = (5 > 0) ? "even" : "odd"; // "even" |
& |
represents 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; |
No comments:
Post a Comment