Operators:
An Operator is a symbol that tells the compiler to perform specific Mathematical or logical Manipulations. Operator takes one or more arguments and produces a new value.
C++ is rich in built-in operators and provide the following types of operators:
Arithmetic Operators.
Arithmetic Operators are used to perform Calculation. There are following arithmetic operators supported by C++ programming language.
Assume that -
int x = 10; int y = 15;
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds Two Operands. | x + y will give 15 |
- | Subtraction | Subtracts second operand from the first. | x - y will give 5 |
* | Multiplication | Multiplies both Operands. in C++ asterisk (*) sign is used for multiplies. | x * y will give 50 |
/ | Division | Divides Numerator by de-numerator. | x / y will give 2 |
% | Modulus | Modulus Operator return the remainder of an integral division. | x % y will give 0 |
Let's have an example to understand all the arithmetic operators available in C++.
#include <iostream>
using namespace std;
main()
{
int x=10;
int y=5;
// endl is used to insert newline.
cout<<"x + y Will give "<<x+y<<endl;
cout<<"x - y Will give "<<x-y<<endl;
cout<<"x * y Will give "<<x*y<<endl;
cout<<"x / y Will give "<<x/y<<endl;
cout<<"x % y Will give "<<x%y<<endl;
return 0;
}
Arithmetic operators
x + y Will give 15
x - y Will give 5
x * y Will give 50
x / y Will give 2
x % y Will give 0
Relational Operators:
Relational Operators establish a relationship between the values of the operands. There are following relational operators supported by C++ programming language
Assume that -
int x = 10; int y = 5;
Operator | Name | Description | Example |
---|---|---|---|
== | Equal to | Checks if the values of two operands are equal or not, if yes then condition become true. Note that the single Equal to (=) operator is used to Assign a value to variables in C++. | (x == y) is not true. |
!= | Not Equal to | Checks if the value of two operands are equal or not, if values are not equal then condition become true. | (x != y) is true. |
> | Greater than | Checks if the value of left Operand is greater than the value of right operand, if yes then condition becomes true. | (x > y) is true. |
< | Less than | Checks if the value of left Operands is less than the value of right operand, if yes then condition become true. | ( x < y) is not true. |
>= | Greater than 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. | (x >= y) is true. |
<= | less than 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. | (x <= y) is not true. |
Let's have an example to understand all the Relational operators available in C++. in the example we will use if and else statement to show relation between operands.
#include <iostream>
using namespace std;
main()
{
int x=10;
int y=5;
if(x==y)
cout<<" x is equal to y: "<<endl;
else
cout<<" x is not equal to y: "<<endl;
if(x>y)
cout<<" x is greater then y: "<<endl;
else
cout<<" x is less then y: "<<endl;
if(x >= y)
cout<<" x is greater than or equal to y: "<<endl;
else
cout<<" x is not greater than or equal to y: "<<endl;
return 0;
}
Relational Operators
x is not equal to y:
x is greater then y:
x is greater than or equal to y:
Logical Operators.
The logical Operator Produce a true or false based on the logical relationship of its arguments. Remember that in C and C++, a statement is true if it has a non-zero value, and false if it has a value of zero. if we print a bool, we'll typically see a "1" for true and "0" for false. There are following logical operators supported by C++ language.
Assume that -
int x = 1; int y = 0;
Operator | Name | Description | Example |
---|---|---|---|
&& | Logical AND | If both the Operands are non-zero, then condition become true. Note that the single Ampersand (&) have another uses in C++. | (x && y) is false. |
|| | Logical OR | If any of the two Operands is non-zero, then condition becomes true. | (x || y) is true. |
! | Logical NOT | Use to reverses the logical state of its Operands. if a condition is true, then Logical NOT operator will make it false. | !(x && y) is true. |
Let's have an example to understand all the Logical operators available in C++. in the example we will use if and else statement to show logical operators.
#include <iostream>
using namespace std;
main()
{
int x=1;
int y=0;
if(x&&y)
cout<<"Condition is true: "<<endl;
else
cout<<"Condition is not true: "<<endl;
if(x || y)
cout<<"Condition is true: "<<endl;
else
cout<<" Condition is not true: "<<endl;
if(!(x&&y))
cout<<"Condition is true: "<<endl;
else
cout<<" Condition is not true: "<<endl;
return 0;
}
Logical Operators
Condition is not true:
Condition is true:
Condition is true:
Bitwise Operators.
The Bitwise Operators allow us to manipulate individual bits in a Number ( Since floating point values use a special internal format, the bitwise operator work only with integral types: char, int and long.) Bitwise Operators Perform Boolean algebra on the corresponding bits in the arguments to produce the result.
Assume that -
int x = 60; int y = 13;
then in binary format x and y will be as follows:
Bitwise Operator works on bits and perform bit-by-bit Operation. The truth Tables for & , | , and ^ are as follows.
p | q | p & q | p | q | p ^ q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
There are following Bitwise operators in C++ programming language: Assume that -
int x = 60; int y = 13;
Operator | Name | Description | Example |
---|---|---|---|
& | Binary AND | Binary AND Operator copies a Bit to the result if it exists in both Operands. | (x & y) will give 12 which is 0000 1100 |
| | Binary OR | Binary OR Operator copies a bit if it exists in either Operand. | (x | y) will give 61 which is 0011 1101. |
^ | Binary XOR | Binary XOR Operator copies the bit if it is set in one Operand but not both. | (x ^ y) Will give 49 which is 0011 0001. |
~ | Binary Ones Complement | Binary Ones Complement Operator is Unary and has the effect of "Flipping" Bits. | ( ~x ) Will give -61 which is 1100 0011 in 2's complement form due to a signed binary Number. |
<< | Binary Left Shift. | The left operands value is moved left by the number of bits specified by the right operand. | x << y will give 240 which is 1111 0000. |
>> | Binary Right Shift. | The left operands value is moved right by the number of bits specified by the right operand. | x >> y will give 15 which is 0000 1111 |
Let's have an example to understand all the Bitwise operators available in C++.
#include <iostream>
using namespace std;
main()
{
unsigned int x = 60; // 60 = 0011 1100
unsigned int y = 13; // 13 = 0000 1101
int z = 0;
z = x & y; // 12 = 0000 1100
cout << "Using Binary AND Operator the value of z is: " << z << endl ;
z = x | y; // 61 = 0011 1101
cout << "Using Binary OR Operator the value of z is: " << z << endl ;
z = x ^ y; // 49 = 0011 0001
cout << "Using Binary XOR Operator the value of z is: " << z << endl ;
z = ~x; // -61 = 1100 0011
cout << "Using Binary Ones Complement Operator the value of z is: " << z << endl ;
z = x << 2; // 240 = 1111 0000
cout << "Using Binary Left shift Operator the value of z is: " << z << endl ;
z = x >> 2; // 15 = 0000 1111
cout << "Using Binary Right shift Operator the value of z is: " << z << endl ;
return 0;
}
Bitwise Operators
Using Binary AND Operator the value of z is: 12
Using Binary OR Operator the value of z is: 61
Using Binary XOR Operator the value of z is: 49
Using Binary Ones Complement Operator the value of z is: -61
Using Binary Left shift Operator the value of z is: 240
Using Binary Right shift Operator the value of z is: 15
Assignment Operators.
Assignment Operator are used to assign a value of right side Operand to left side Operand. There are following Assignment Operators supported by C++ programming language:
Operator | Name | Description | Example |
---|---|---|---|
= | Assignment | Simple Assignment Operator Assign Values from right side Operands to left side Operand. | z = x + y will assign value of x + y into z. |
+= | Add and Assignment | Add and Assignment Operator adds right operand to the left operand and assign the result to left Operand. | z += x is equivalent to z = z + x. |
-= | Subtract and Assignment | Subtract and Assignment Operator subtracts right Operand from the left Operand and assign the result to left operand. | z -= x is equivalent to z = z - x. |
*= | Multiply and Assignment | Multiply and Assignment Operator multiplies right operand with left operand and assign the result to left operand. | z *= x is equivalent to z = z * x. |
/= | Divide and Assignment. | Divide and Assignment operator divides left operand with the right operand and assign the result to left operand. | z /= x is equivalent to z = z / x |
%= | Modulus and Assignment. | Modulus and Assignment operator takes modulus using two operands and assign the result to left Operand. | z %= x is equivalent to z = z % x |
<<= | Left Shift and Assignment. | Left shift and assignment Operator . | z <<= 2 is equivalent to z = z << 2 |
>>= | Right shift and Assignment. | Right shift and assignment Operator | z >>= 2 is equivalent to z = z >> 2 |
&= | Bitwise AND Assignment. | Bitwise AND and assignment Operator:. | z &= x is equivalent to z = z & x |
^= | Bitwise XOR Assignment. | Bitwise Exclusive XOR and assignment Operator:. | z ^= x is equivalent to z = z ^ x |
|= | Bitwise OR Assignment. | Bitwise Exclusive OR and assignment Operator:. | z |= x is equivalent to z = z | x |
#include <iostream>
using namespace std;
main()
{
int x=12;
int z;
z = x;
cout<<" Using \"=\" operator: value of z is : "<<z<<endl;
z += x;
cout<<" Using \"+=\" operator: value of z is : "<<z<<endl;
z -= x;
cout<<" Using \"-=\" operator: value of z is : "<<z<<endl;
z *= x;
cout<<" Using \"*=\" operator: value of z is : "<<z<<endl;
z /= x;
cout<<" Using \"/=\" operator: value of z is : "<<z<<endl;
z %= x;
cout<<" Using \"%=\" operator: value of z is : "<<z<<endl;
z <<= 2;
cout<<" Using \"<<=\" operator: value of z is : "<<z<<endl;
z >>= 2;
cout<<" Using \">>=\" operator: value of z is : "<<z<<endl;
z &= 2;
cout<<" Using \"&=\" operator: value of z is : "<<z<<endl;
z ^= 2;
cout<<" Using \"^=\" operator: value of z is : "<<z<<endl;
z |= 2;
cout<<" Using \"|=\" operator: value of z is : "<<z<<endl;
return 0;
}
Assignment Operators
Using "=" operator: value of z is : 12
Using "+=" operator: value of z is : 24
Using "-=" operator: value of z is : 12
Using "*=" operator: value of z is : 144
Using "/=" operator: value of z is : 12
Using "%=" operator: value of z is : 0
Using "<<=" operator: value of z is : 0
Using ">>=" operator: value of z is : 0
Using "&=" operator: value of z is : 0
Using "^=" operator: value of z is : 2
Using "|=" operator: value of z is : 2
“
In C++ there is an special meaning of double Quotes ( " " ). So in order to show double Quotes in C++ Program, we need a special method to display it: (i.e. |" ) back slash + double Quotes:
Increment and Decrements Operators.
C++ Provides two Unusual Operators for increment and decrements variables: The Unusual aspect is that ++ and -- may be used either as Prefix Operators or Postfix Operators. as shown in the table: Assume that -
int x = 10; int y = 5;
Operator | Name | Description | Example |
---|---|---|---|
++ | Increment | Increment Operator increase value by one. | x++ will give 11. |
x++ | Postfix Increment | Postfix Increment Operator increase value by one after its value has been used | if y = x++, then y will be 10. because first assignment operator store the value of x (i.e 10) to y, then increment x by 1. |
++x | Prefix Increment | Prefix Increment Operator increase value by one before its value is used. | if y = ++x, then y will be 11. because first x will be increment by 1 then it will store in y. |
-- | Decrements | Decrements Operator decreases integer value by one. | x-- will give 9 |
x-- | Postfix Decrements | Postfix Decrements Operator Decreases value by one after its value has been used | if y = x--, then y will be 10. because first assignment operator store the value of x (i.e 10) to y, then decrements x by 1. |
--x | Prefix decrements | Prefix decrements Operator decreases value by one before its value is used. | if y = --x, then y will be 9. because first x will be Decrements by 1 then it will store in y. |
Misc Operators.
There are some more useful Operators that C++ supports:
Operator | Description |
---|---|
Sizeof | Sizeof Operator returns the size of a variable. For example: sizeof(a), where "a" is an integer, and will return 4. (Read More...) |
condition ? x:y | Conditional Operator (?). work same like if and else statement: If condition is true then it return value of x otherwise returns value of y. (Read More...) |
, | Comma Operator causes a sequence of Operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list. |
.(dot) and -> (arrow) | Member Operator are used to reference individual Members of Classes, Structure, and Unions. (Read More...) |
cast | Casting Operators convert one data type to another. For example: int(2.2000) would return 2. |
& | Pointer Operator "&" returns the address of a variable. For example: &x will give the actual address of the variable. |
* | Pointer operator * is pointer to a variable. For Example: *var; will pointer to a variable var. (Read More...) |
Operators Precedence in C++.
Precedence: The Precedence Mean which Operator will be evaluated first and which will be evaluated after that and so on.
Associativity: When an expression contains two Operators with the same Precedence, which should be applied first.
When different Operators are used in the same expression, the normal rules of arithmetic apply. All C++ Operators have a Precedence and Associativity
In Expression, the Parentheses () are used to force the evaluation Order. The Operator in the Parentheses () are Evaluated first. If there are nested Parentheses then the inner most is evaluated first. For Example:
int x = 2 + 3 * 2;
Here x is assigned 8, not 10. Because Multiplication (*) has higher Precedence that addition (+), so it first get multiplied with 3*2 and then adds 2 into it. Let's have another Example.
int x = (2 + 3) * 2;
Here x is assigned 10, not 8. Because Parentheses () has higher Precedence that any other Operator. so addition will be evaluated first and then it will multiply with 2.
Here, Operators with the highest Precedence appear at the top of the Table, and those with the lowest appear at the bottom. Within an Expression, higher precedence Operators will be evaluated first.
Category | Operator | Associativity |
---|---|---|
Postfix | () [] -> . ++ -- | Left to Right |
Unary | + - ! ~ ++ -- (type)* & sizeof | Right to Left |
Multiplicative | * / % | Left to Right |
Additive | + - | Left to Right |
Shift | << >> | Left to Right |
Relational | < <= > >= | Left to Right |
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 | = += -= *= /= %= >>= <<= &= ^= \= | Right to Left |
Comma | , | Left to Right |
“
Paying Attention to Detail: In Programming, The detail matter. this is very Important to analyzes the Problem statement very carefully and in detail. We should pay attention to the calculations involved in the Program, its flow, the Operator Precedence and Associativity. just a minor mistake could result in a logical error, which is difficult to find out. So be careful while working with Operators: