C++ - Operators
Operators are used to perform operation on a single operand or two operands. Operators in C++ can be categorized as follows:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- Bitwise operators
- Other operators
Arithmetic operators
Arithmetic operators are used to perform arithmetic operations on two operands.
Operator | Name | Description |
---|---|---|
+ | Addition | Add two values |
- | Subtraction | Subtract one value from another |
* | Multiplication | Multiply two values |
/ | Division | Divide one value by another |
% | Modulus | Returns remainder of division operation |
Example
Assignment operators
Assignment operators are used to assign values of right hand side expression to left hand side operand.
Operator | Expression | Equivalent to | Description |
---|---|---|---|
= | a = 5 | a = 5 | Example |
+= | 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 | |
&= | a &= b | a = a & b | More Info |
|= | a |= b | a = a | b | More Info |
^= | a ^= b | a = a ^ b | More Info |
>>= | a >>= b | a = a >> b | More Info |
<<= | a <<= b | a = a << b | More Info |
Comparison operators
Comparison operators are used to compare values of two operands. It returns true when values matches and false when values doen not match.
Operator | Description |
---|---|
== | Equal |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example
Increment/Decrement operators
Increment and decrement operators are used to increase and decrease the value of variable.
Operator | Name | Description | Example |
---|---|---|---|
++x | Pre-increment | Increases the value of x by 1, then returns x. | Example |
x++ | Post-increment | Returns x, then increases the value of x by 1. | |
--x | Pre-decrement | Decreases the value of x by 1, then returns x. | Example |
x-- | Post-decrement | Returns x, then decreases the value of x by 1. |
Logical operators
Logical operators are used to combine two or more conditions.
Operator | Name | Description |
---|---|---|
&& | AND | Returns True when all conditions are true |
|| | OR | Returns True when any of the conditions is true |
! | NOT | Returns opposite boolean result |
More Info
Bitwise operators
Bitwise operators are used to perform bitwise operations on two operands.
Operator | Name | Description | More Info |
---|---|---|---|
& | AND | Returns 1 if both bits at the same position in both operands are 1, else returns 0. | More Info |
| | OR | Returns 1 if one of two bits at the same position in both operands is 1, else returns 0. | More Info |
^ | XOR | Returns 1 if only one of two bits at the same position in both operands is 1, else returns 0. | More Info |
~ | NOT | Reverse all the bits. | More Info |
>> | Right shift | The left operand is moved right by the number of bits present in the right operand. | More Info |
<< | Left shift | The left operand value is moved left by the number of bits present in the right operand. | More Info |
Other operators
The below table describes other operators supported by C++:
Operator | Description |
---|---|
sizeof() | Returns size of a data type, constants or variable. |
ternary operator (?:) | Returns one of the two values based on value of boolean expression. |