Operators in C++
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.
Like, int X=10+20;
int Y=20-10;
Operators in C++ are classified into 6 types:
1.Arithmetic Operators
2.Assignment Operators
3.Relational Operators
4.Logical Operators
5.Bitwise Operators
6.Other Operators
Arithmetic Operators :
Arithmetic operators are used to perform arithmetic operations on varibales and data.
Example:
A=10+20;
Here, the (+) operators is used to add two variables a and b and the result is stored in variable c.
Similarly there are various other arithmetic operators in C++
Operators Operation Example
+ Addition of two variables a=b+c;
- Subtraction of two variables a=b-c;
* Multiplication of two variables a=b*c;
/ Division of two variables a=b/c;
% Modulo after Operation a=b%c;
Example:
#include<iostream> using namespace std; int main() { int a=20,b=10; // printing the sum of a and b cout<<"The Sum of a and b is(a+b)="<<a+b<<endl; //Printing the difference of a and b cout<<"The difference of a and b is(a-b)="<<a-b<<endl; //Printing the product of a and b cout<<"The product of a and b is(a*b)="<<a*b<<endl; //Printing the division of a and b cout<<"The division of a and b is (a/b)"<<a/b<<endl; //Printing the modulo of a and b cout<<"The modulo of a and b is (a%b)"<<a%b<<endl; return 0; } O/p: The Sum of a and b is(a+b)=30 The diffrence of a and b is(a-b)=10 The product of a and b is(a*b)=200 The division of a and b is(a/b)=2 The modulo of a and b is(a%b)=0
Assignment Operators :
- Assignment operators are used to assign value to a variable.
- The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value.
- The value on the right side must be of the same data-type of variable on the left side otherwise the compiler will raise an error.
Different types of assignment operators are shown below:
1.Operator:(=)
It is just a simple assignment operator,assign the value from right side operands to left side operands.
Example:
int A=5;
int b=20;
int ch='a';
2.Operator:(+=)
It just a combination of Addition Operator and assignment operator.It first add the value of left operand to right operand.
Example:
int a=10;
a+= 5;//it is alos written as a=a+5;
3.Operator:(-=)
It just a combination of Subtraction Operator and assignment operator.It first subtracts the value of left operand to right operand.
Example:
int a=10;
a-= 5;//it is alos written as a=a-5;
4.Operator:(*=)
It just a combination of Multiplication Operator and assignment operator.It first multiplys the value of left operand to right operand.
Example:
int a=10;
a*= 5;//it is alos written as a=a*5;
5.Operator:(/=)
It just a combination of Division Operator and assignment operator.It first divides the value of left operand to right operand.
Example:
int a=10;
a/= 5;//it is alos written as a=a/5;
6.Operator:(%=)
It just a combination of modulo Operator and assignment operator.It first takes modulus of two operands and assigns the value.
Example:
int a=10;
a%= 5;//it is alos written as a=a%5;
Relational Operators :
Relational Operators are used to check the relationship between two operands.
Example:
1.Is Equal To(==):
Checks if the values of two operands are equal or not, if yes then condition becomes true.
Example
(3==3) returns True
(1==2) returns false
2.Not Equal To(!=):
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
Example
(1!=1) returns false
(1!=2) returns true
3.Greater Then(>):
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
Example:
(2>3) returns false
(10>5) returns true
4.Less Then(<):
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
Example:
(2<3) returns true
(3<2) returns false
5.Greater Then or Equal To(>=):
Checks if the value of left operand is greater than or equal to the value of right operand,
if yes then condition becomes true.
Example:
(1>=1) returns true
(1>=2) returns false
6.Less Then or Equal To(<=):
Checks if the value of left operand is less than or equal to the value of right operand,
if yes then condition becomes true.
Example:
(1<=1) returns true
(2<=1) returns false
Logic Operators :
- logic Operators are used to check whether an exppression is 'True' or 'Flase'.
- If the expression is true, it returns 1 whereas if the expression is false, it returns 0.
and We have 3 types of logical operators
1.Logical AND
2.Logical OR
3.Logical NOT
Logical AND
Symbol='&&'
It returns true when both the expression are true,otherwise it returns false.
Syntax:
expression1 && expression2
Logical OR
Symbol='||'
It returns true when atleast one of the expression(operand) is ture.
Syantax:
expression1 || expression2
Logical NOT
Symbol='!'
It return true only if expression(operands) is false.
Synatx:
!expression
Example:
#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
Bitwise Operators
- Bitwise operators works on bits and perform bit-by-bit operation.
- Firstly it converts the operands into bits and performs operations
and we have 6 operators in Bitwise operators
1.Bitwise AND(&)
The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
2.Bitwise OR(|)
The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.
3.Bitwise XOR(^)
The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
4.Left shift(<<)
The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
5.Right shift(>>)
The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
6.Bitwise NOT(~)
The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it.
Example:
#include <iostream>
using namespace std;
int main()
{
unsigned char a = 5, b = 9,int num=35;
cout << "a & b = " << (a & b) << endl;
cout << "a | b = " << (a | b) << endl;
cout << "a ^ b = " << (a ^ b) << endl;
cout << "b << 1= " << (b << 1)<<endl;
cout << "b >> 1= " << (b >> 1)<<endl;
cout << "~( << num1 << ) = " << (~num1) << endl;
return 0;
}
-----------output:------------
a&b = 1
a|b = 13
a^b = 12
b<<1 = 18
b>>1 = 4
~(num)=-36
AUTHOR : Rakshit Joshi [ Linkedin Profile ]
For Videos Join Our Youtube Channel: Join Now