C++ Operators
Definition:
An operator is a symbol that operates on a value to perform specific mathematical or logical computations.They form the foundation of any programming language.In C++, we have built-in operators to provide the required functionality.
Types of Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Arithmetic Operators
Name | Operator | Example |
---|---|---|
Addition | + | a+b |
Subtraction | - | a-b |
Multiplication | * | a*b |
Division | / | a/b |
Modulus | % | a%b |
Increment | ++ | a++ or ++a |
Decrement | -- | a-- or --a |
Assignment Operators
Operator | Example | Same as |
---|---|---|
= | a=10 | a=10 |
+= | a+=10 | a=a+10 |
-= | a-=10 | a=a-10 |
*= | a*=10 | a=a*10 |
/= | a/=10 | a=a/10 |
%= | a%=10 | a=a%10 |
//= | a//=10 | a=a//10 |
**= | a**=10 | a=a**10 |
&= | a&=10 | a=a&10 |
|= | a|=10 | a=a|10 |
^= | a^=10 | a=a^10 |
//= | a//=10 | a=a//10 |
>>= | a>>=10 | a=a>>10 |
<<= | a<<=10 | a=a<<10 |
Comparison Operators
Name | Operator | Example |
---|---|---|
Equal | == | a==b |
Not Equal | != | a!=b |
Greater Than | > | a>b |
Less Than | < | a<b |
Greater Than or Equal to | >= | a>=b |
Less Than or Equal to | <= | a<=b |
Logical Operators
Name | Description | Example |
---|---|---|
and | Returns True if both statements are true | a<5 and b<10 |
or | Returns True if one of the statements is true | a<10 or b<5 |
not | Reverse the result, returns False if the result is true | not(a<5 and b<10) |
Bitwise Operators
Name | Description | Example |
---|---|---|
AND | Sets each bit to 1 if both bits are 1 | a & b |
OR | Sets each bit to 1 if one of two bits is 1 | a | b |
XOR | Sets each bit to 1 if only one of two bits is 1 | a ^ b |
NOT | Inverts all the bits | ~a |
Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off | a<<3 |
Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | a>>3 |