operators in c++
In this article, you will learn about the operators in CPP.how to use and what are the functions of operators in c++.
The following are the logical operators that are available in c++ language.
operator
An operator is a symbol that tells the computer to perform a specific function. The function will be mathematical, and logical manipulation. There are many built-in operators in the CPP . The following are the types of operators that are using in c++.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
Arithmetic Operators
Arithmetic operators use for the mathematical arithmetic tasks. The following are the operators that are supported by c++.
Addition operator(+):
The symbol(+) is used to adding two operands. The operands may be integers, float, and double.
An example will be clear how to use this operator:
Subtract operator(-)
The symbol(-) is used to subtract the two operands. Subtract the second operand from the first operand.
An example will be helping:
Multiple operator(*)
This symbol(*) is used to multiple the operands. This is a binary operator which multiple both operator.
Example:
Divide operator(/)
A (/) symbol is used for the divide the numerator by de-numerator. This is also a binary operator , needs two operators.
Example1:
Modulus operator(%)
This operator uses for finding the remainder after the integer division. This is very useful when we work in different number systems.
Example1:
Increment operator(++)
This operator increases the integer value to one.It is very useful in loop programming.
Example1:
Decrement operator(--)
It is opposite to the increment operator(++) and used for decreasing the integer value by one.This is also very helpful during loop programming.
For more detail about pre and post increment.
Example1
#include<iostream>
using namespace std;
int main()
{
int a=50 ,b=40;
cout<<" addition operator ="<<a+b<<endl;
cout<<" subtraction operator ="<<a-b<<endl;
cout<<" multiple operator ="<<a*b<<endl;
cout<<" division operator ="<<a/b<<endl;
cout<<" modulous operator ="<<a%b<<endl;
cout<<" increment operator ="<<++a<<endl;
cout<<" decrement operator ="<<--a<<endl;
return 0;
}
output
addition operator =90
subtraction operator =10
multiple operator =2000
division operator =1
modulous operator =10
increment operator =51
decrement operator =50
Relational operators:
Relational operators use for the comparison between two operands. Comparison needed during the programming for example if statement. The following are the relational operators that are available in the c++ language.
Equal operator(==)
This operator compares the values of two operands and if both operands equal then return true but if both operands are not equal then return false.
Example2:
Not equal(!=)
It checks the value of two operands are equal or not equal,if not equal then return true and return false if both operands equal.
Example2:
Greater than (>)
If the value of the left operand is greater than the value of the right operand then return true. If right operand greater then false return.
Example2:
Less than(<)
If the value of left operand less than the right operand then returns true otherwise return false.
Example2:
Greater than and equal to(>=)
This operator checks the left operand greater or equal to the right operand. It returns true if left operand greater or equal to right otherwise return false.
Example2:
Less than and equal to(<=)
It checks the left operand less than or equal to the right operand.It returns true if left operand less than or equal to right and otherwise return false.
Example2:
EXAMPLE2
#include<iostream>
using namespace std;
int main()
{
int a=50 ,b=40;
cout<<" equal operator ="<<(a==b)<<endl;
cout<<" not equal operator ="<<(a!=b)<<endl;
cout<<" greater than operator ="<<(a>b)<<endl;
cout<<" less than operator ="<<(a<b)<<endl;
cout<<" greater than or equal to operator ="<<(a>=b)<<endl;
cout<<" less than or equal to operator ="<<(a<=b)<<endl;
return 0;
}
output
equal operator =0
not equal operator =1
greater than operator =1
less than operator =0
greater than or equal to operator =1
less than or equal to operator =0
Logical operators
The following are the logical operators that are available in c++ language.
AND operator(&&)
This is a binary operator which returns true if both the operands are non-zero(which means both operands are true). If one of them false then return false.
EXAMPLE3
OR operator(||)
OR operator returns true if one of the operands is non-zero(which means true) otherwise return false.
EXAMPLE3
NOT operator(!)
logical NOT operator uses to reverse the state of its operand. If statement true but by use (!) will make false.
EXAMPLE3
#include<iostream>
using namespace std;
int main()
{
int a=1 ,b=0;
cout<<" AND operator ="<<(a&&b)<<endl;
cout<<" OR operator ="<<(a||b)<<endl;
cout<<" NOT operator ="<<(!a)<<endl;
return 0;
}
output
AND operator =0
OR operator =1
NOT operator =0
Assignment Operators
Assign(=)
The assignment operator assigns the values of the right operand to the left operand. The result is left operand have the same value as of right .
EXAMPLE4
ADD AND ASSIGN(+=)
This operator firstly adds the value of the right in the value of left operand, then assigns the result in the left operand.
EXAMPLE4
Subtract and assignment(-=)
It subtracts the value of right from the left operand and then assign the result to the left operand .
EXAMPLE4
Multiple and assignment(*=)
This operator multiple the value of left operand with the right operand and then store the result in the left operand.
EXAMPLE4
Divide and assign(/=)
divide and assignment operator divide the value of left operand by value of right and then assign the result to the left operand.
EXAMPLE4
Left shift and assignment(<<=)
It shifts the value of operand toward the left and assigns the value to the same operand.
EXAMPLE4
RIGHT SHIFT AND ASSIGNMENT(>>=)
It shifts the value of operand toward the right and then assigns value to the same operand.
EXAMPLE4
Bitwise and assignment(&=)
This is performed bitwise AND operation with left operand and assign the value in the left operand.
EXAMPLE4
Bitwise exclusive OR operator(^=)
It also performs bitwise exclusive OR operation on the left operand and assigns the value to the left operand.
EXAMPLE4
Bitwise inclusive OR operator(|=)
I perform inclusive OR operation on the left operand and then assign the value to the left operand.
EXAMPLE4
#include<iostream>
using namespace std;
int main()
{
int a=50 ,b=40;
a=b;
cout<<" assign operator ="<<a<<endl;
a+=b;
cout<<" add and assign operator ="<<a<<endl;
a*=b;
cout<<" multiple and assign operator ="<<a<<endl;
a/=b;
cout<<" division and assign operator ="<<a<<endl;
a<<=b;
cout<<" left shift and assign operator ="<<a<<endl;
a>>=b;
cout<<" right shift and assign operator ="<<a<<endl;
a&=b;
cout<<" bitwise and and assign operator ="<<a<<endl;
a^=b;
cout<<" bitwise exclusive or and assign operator ="<<a<<endl;
a|=b;
cout<<" bitwise inclusive or and assign operator ="<<a<<endl;
return 0;
}
Output
assign operator =40
add and assign operator =80
multiple and assign operator =3200
division and assign operator =80
left shift and assign operator =20480
right shift an assign operator =80
bitwise and assign operator =0
bitwise exclusive or and assign operator =40
bitwise inclusive or and assign operator =40
Bitwise operators
By name bitwise show that operations performed on the bits.for example:
A=10
B=20
Bits of A and B
A=0000 1010
B=0001 0100
~A=1111 0101
A&B=0000 0000
A|B=0001 1110
A^B=0001 1110
AND operator(&)
AND operator multiple the bits of two operands and produce the result.
OR operator(|)
OR operator adds the bits of the operand and produces the result.
XOR operator(^)
This operator produces 1 if both bits of the operand is not the same. and produce zero if both bits are the same.
Complement operator( ~)
This operator is unary operator and flipping the bits of he operand.
Shift left (<<)
Shift left operator move the bites of operand toward the left side and add zero from the right side.
Shift right(>>)
Shift right operator move the bites of operand toward the right side and add zero from the left side.
Examples
#include<iostream>
using namespace std;
int main()
{
int a=50 ,b=40,c;
c=a&b;
cout<<" bitwise AND operator ="<<c<<endl;
c=a|b;
cout<<" bitwise OR operator ="<<c<<endl;
cout<<" bit wise complement operator ="<<(~a)<<endl;
c=a^b;
cout<<" exclusive OR operator ="<<c<<endl;
a>>=2;
cout<<" right shift ="<<a<<endl;
b<<=2;
cout<<" left operator ="<<b<<endl;
return 0;
}
output
bitwise AND operator =32
bitwise OR operator =58
bit wise complement operator =51
exclusive OR operator =26
right shift =12
left operator =160
Please write the comment,if you find anything incorrect,any problem in the above topic or you want to share more information about the above topic.
nice work dear.c++
ReplyDeletehttps://basicprogramingknowledge.blogspot.com/