Operators in C++
Operators are the integral parts of any programming language. Operators are the special symbols that mean to carry out specific
operations on their operands. C++
provide a rich set of built-in operators for different operations.
There are different types of operators defined in C++, these are as mentioned below.
- Arithmetic Operators
- Assignment Operators
- Increment & Decrement Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Conditional Operator
- Shift Operator
- Comma Operator
- SizeOf Operator
- Dot and Arrow Operator
- Pointer and AddressOf Operator
Now let see each of them in detail.
Arithmetic Operators:
Arithmatic operator are used to perform basic arithmatic operations such as add, subtract, multiply etc.
Operator | Description | Example |
---|---|---|
+ | perform addition between two operands | value1 + value2 |
- | perform subtraction between two operands | value1 - value2 |
* | perform multiplication between two operands | value1 * value2 |
/ | perform division between two operands and returns Quotient | value1 / value2 |
% | perform division between two operands and returns Remainder | value1 % value2 |
Arithmetic operator can be classified into two types:
Unary Operators: These are the operators which works on single operand or value.
Ex: ++ , -–
Syntax: ++value, value-- etc.
Binary Operators: These are the operators which works on two operands or values
Ex: + , – , * , /, %
Assignment Operators:
Assignment operators are used to assign the values to the operand or variable.
Operator | Description | Example |
---|---|---|
= | Assign value of Right hand side operand to the left side operand | value1 = value2 |
+= | perform addition of two operands and assign the value to the first operand | value1 += value2; Addition of value1 and value2 and assign the result to value1 |
-= | perform subtraction of two operands and assign the value to the first operand | value1 -= value2; Subtraction of value1 and value2 and assign the result to value1 |
*= | perform multiplication of two operands and assign the value to the first operand | value1 *= value2; Multiplication of value1 and value2 and assign the result to value1 |
/= | perform division of two operands and assign the value to the first operand | value1 /= value2; Division of value1 and value2 and assign the result to value1 |
%= | perform division of two operands and assign the value to the first operand | value1 %= value2; Modulus of value1 and value2 and assign the result to value1 |
&= | perform bitwise 'AND' and assign the value | value1 &= 2; Bitwise 'AND' of value1 and '2' and assign the result to value1 |
|= | perform bitwise 'OR' and assign the value | value1 |= 2; Bitwise 'OR' of value1 and '2' and assign the result to value1 |
^= | perform bitwise exclusive 'OR' and assign the value | value1 ^= 2; Bitwise exclusive 'OR' of value1 and '2' and assign the result to value1 |
<<= | perform left shift and assign the value | value1 <<= 2; Leftshift two times the value1 and assign the result to value1 |
>>= | perform right shift and assign the value | value1 >>= 2; Rightshift two times the value1 and assign the result to value1 |
Increment & Decrement Operators:
C++ provides two useful operators known as increment ("++") and decrement ("--") operator. These are also known as pre and
post-arithmetic operator. These are the unary operator and
works with a single operand.
Operator | Description | Example |
---|---|---|
++ | perform increment of the operand | ++value; [Prefix] value++; [Postfix] |
-- | perform decrement of the operand | --value; [Prefix] value--; [Postfix] |
There is a difference between its prefix and postfix usage in the expression. When the increment or decrement operator is used as a
prefix to the
operand then increment/decrement operation is performed before obtaining the value of the operand. And if we put them
in postfix then the value of the operand is fetched first
and then increment/decrement operation will be done. Let check the below
example.
Ex. x = 5;
y = ++x // Prefix Notation
Here the value of y obtained will be "6".
x = 5;
y = x++ // Postfix Notation
Here the value of y obtained will be "5". However, in both cases, the value of x will be set to 6.
Relational Operators:
These operators are used to evaluate the relationship between the two operands. These are also known as Comparison operators.
Depending on the result of the comparison the expression
will return the value true/false or 0 or 1.
Operator | Description | Example |
---|---|---|
> | Check LHS value is greater than RHS value |
Let value1 = 5 and value2 = 10 value1 > value2 ; This expression will give False or 0 |
>= | Check LHS value is greater than or equal to RHS value |
Let value1 = 5 and value2 = 5 value1 >= value2 ; This expression will give True or 1 |
< | Check LHS value is less than RHS value |
Let value1 = 5 and value2 = 10 value1 < value2 ; This expression will give True or 1 |
<= | Check LHS value is less than or equal to RHS value |
Let value1 = 5 and value2 = 10 value1 <= value2 ; This expression will give True or 1 |
== | Check LHS value is equal to RHS value |
Let value1 = 5 and value2 = 10 value1 == value2 ; This expression will give False or 0 |
!= | Check LHS value is not equal to RHS value |
Let value1 = 5 and value2 = 10 value1 != value2 ; This expression will give True or 1 |
*Note: In C++ "True" or "False" will automatically be converted to "1" or "0" respectively.
Logical Operators:
Logical Operator plays an important role in decission making statement and in different type of looping conditions. They are used
to to combine twodifferent
expressions. For example if two expression are combined with the AND operator than validity of both
will be considered and if they are combined with
OR operator than validity of any one of them is considered. In many of the
statements Relational and Logical operator works together.
Operator | Description | Example |
---|---|---|
&& | perform Logical "AND". Returns TRUE only when all of the expression are true. | exp1 && exp2 |
|| | perform Logical "OR". Returns TRUE when any of the expression is true. | exp1 && exp2 |
! | perform Logical "NOT". Will invert the value of the expression. Check the truth table below. |
!exp1 |
*Note: In C++ "True" or "False" will automatically be converted to "1" or "0" respectively.
Operations of the logical operator can be shown in the below truth table using value 1 and 0.
Value "a" | Value "b" | a && b | a || b | !a | !b |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 1 | 1 |
0 | 1 | 0 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 0 | 1 |
1 | 1 | 1 | 1 | 0 | 0 |
Bitwise Operators:
Bitwise operators are used to perform bit-by-bit operation between the operands.
For futher explanation let us take two variables val1 and val2 having values "127" and "111" respectively. Then their
binary
representation will be as below.
val1 = 0111 1111
val2 = 0110 1111
Now let check the result of different types of bitwise operator on these variables.
Operator | Description | Example |
---|---|---|
& | perform Bitwise "AND". | val1 & val2; Returns value 0110 1111 equivalent to decimal value 111 |
| | perform Bitwise "OR". | val1 | val; Returns value 0111 1111 equivalent to decimal value 127 |
^ | perform "exclusive OR (XOR)". | val1 ^ val; Returns value 0001 0000 equivalent to decimal value 16 |
~ | perform "One's complement (NOT)". | ~val2; Returns value 1001 0000 equivalent to decimal value -111 |
Truth table for the bitwise operations is as below.
Value of "var1" | Value of "var2" | var1 & var2 | var1 | var2 | var1 ^ var2 |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
Points to remember:-
- Bitwise AND is a way to clear a bit. It means if any bit in either of the operand is "0" then the resultant bit will set to "0".
- Bitwise OR is a way to set a bit. It means if ant bit in either of the operand is "1" then the resultant bit will set to "1".
- In case of Exclisive OR (XOR) the bit will be set only if the bits of two operand being compared are different. It means that if one bit is "0"
and other is "1" then only the resultant bit will be "1" else it will be "0" if both bits are same.
Conditional Operator:
Conditoinal operator is the one of the powerful operator that replace the if-then-else statements. It is a ternary operator, it means
it operates on three operands.
The syntax for the conditional operator is as below.
< condition>? < statement1> : < statement2>
In this it will first check the condition. If the condition returns true then "statement1" is executed else "statement2" is executed.
Example for this is as below
int test = (50 > 10) ? 50 : 10;
cout << test;
In this code snippet it will first check if 50 is greater then 10. If it is true it will assign the value 50 to "test" else it will assign value 10.
So here on printing the value of
"test" we will get the result "50".
Shift Operator:
Shift Operators are generally used to shift bits of any variable. It is is also a type of bitwise operator.
Different type of shift operator is as below.
Operator | Description | Example |
---|---|---|
<< | Left Shift Operator | val << 2; This will left shift the value of val by 2 bits |
>> | Right Shift Operator | val >> 2; This will right shift the value of val by 2 bits |
Comma Operator:
Comma operator has somewhat the same meaning as the word "and" in general english. It is used to join several expression together
and will be helpful in executing sequence of operation.
Example of this is as below.
int i = (a=10 , a+20);
In this we will get the result value of "i " as 30 and "a " is 10.
The left side of the comma operator is always evaluated as void. It means that the value of the expression on the right of the comma
operator will be the final value of the complete
comma-separated expression. That's why in the above example we are getting the final
value of "i " as 30.
SizeOf Operator:
" sizeof " is a unary operator that will return a lenght or size of the variable is bytes.
Example:
int val = 50;
cout << sizeof(val);
Here we get the result as 4, as the variable " val " is of type integer and size of integer is 4 byte.
Dot and Arrow Operator:
The (.)dot and (->)arrow operator are generally used to access the members of the Structres, Unions and Classess. When we deals
directly with the instance of the these data types then we have to
use the (.)dot operator and when we deals with pointer to these
datatypes then we have to use (->)arrow operator.
Pointer and AddressOf Operator:
A pointer is the address of some object. A pointer variable is a variable which is specifically defined to hold the pointer to an object.
The (*)pointer operator is unary operator which returns the value of the variable located at the specifc address.
Whereas (&)AddressOfoperator is also a unary operator which returns the memory address of the operand.
Below is the example for this one.
Example:-
int value = 100;
int val1 = &value;
int val2 = *val1;
cout<< val1;
cout<< val2;
Here "val1 " will store the memory location or memory address of the variable "value " and "val2 " will store the value of the variable
present in the memory location
at which "val " is pointing to.
So when we print the output then "val2 " will gives value "100 " and "val1 " will give the memory location which will be something
like "0x01a0 ".
Precedence Of Operators:
Whenever we work on an expression that contains multiple operators then operator precedence will play an important which doing the
final execution of the expression. Operator precedence defines the
order in which the mathematical expression will be evaluated. An
operator with higher precedence will be evaluated first and the operator with the lowest precedence will operate last.
For example we are having an expression as, x= 10+5*2. Here first multiplication of 5 and 2 will be done and then its result will be added
to 10, because as per the BODMAS rule multiply is having
a higher priority than the addition. So we get the result as 20. The same logic
will be followed in C++ for operator precedence.
Also if an expression contains the operator with the same precedence then the execution will follow the operator associativity defined for
that operator group. An operator having left-associativity will
be executed from left to right and an operator with right-associativity will be
executed from right to left.
The table below summarizes the details about the precedence and associativity of the different operators in C++. Each row
contains the operators with the same precedence and precedence decreases from highest to
lowest from the top row to bottom row.
Precedence | Category | Operator | Associativity |
---|---|---|---|
Highest | Postfix | () [] -> . ++ -- | Left to Right |
Unary and Prefix | ! ~ ++ -- (type) * & sizeof | Right to Left | |
Arithmetic (Multiply and Division) | * / % | Left to Right | |
Arithmetic (Add and Subtract) | + - | Left to Right | |
Shifting | << >> | Left to Right | |
Comparision or Relational | < <= > >= | Left to Right | |
Comparision or Relational(Equality) | == != | Left to Right | |
Bitwise AND | & | Left to Right | |
Bitwise XOR | ^ | Left to Right | |
Bitwise OR | | | Left to Right | |
Logical AND | && | Left to Right | |
Logical OR | || | Left to Right | |
Conditional | ?: | Right to Left | |
Assignment | = += -= *= /= %= &= etc. | Right to Left | |
Lowest | Comma | , | Left to Right |