C++ Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is equipped with a rich set of operators, and it provides the following types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
This chapter will introduce arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators one by one.
Arithmetic Operators
The following table shows the arithmetic operators supported by C++.
Assume variable A holds 10 and variable B holds 20, then:
Operator | Description | Example |
---|---|---|
+ | Adds two operands | A + B will give 30 |
- | Subtracts the second operand from the first | A - B will give -10 |
* | Multiplies both operands | A * B will give 200 |
/ | Divides numerator by denominator | B / A will give 2 |
% | Modulus Operator and remainder of after an integer division | B % A will give 0 |
++ | Increment operator | A++ will give 11 |
-- | Decrement operator | A-- will give 9 |
Example
Consider the following example to understand the available arithmetic operators in C++.
Copy and paste the following C++ program into a file named test.cpp, compile, and run the program.
Example
#include <iostream>
using namespace std;
int main()
{
int a = 21;
int b = 10;
int c;
c = a + b;
cout << "Line 1 - Value of c is " << c << endl;
c = a - b;
cout << "Line 2 - Value of c is " << c << endl;
c = a * b;
cout << "Line 3 - Value of c is " << c << endl;
c = a / b;
cout << "Line 4 - Value of c is " << c << endl;
c = a % b;
cout << "Line 5 - Value of c is " << c << endl;
int d = 10; // Test increment and decrement
c = d++;
cout << "Line 6 - Value of c is " << c << endl;
d = 10; // Reassign value
c = d--;
cout << "Line 7 - Value of c is " << c << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 10
Line 7 - Value of c is 10
Relational Operators
The following table shows the relational operators supported by C++.
Assume variable A holds 10 and variable B holds 20, then:
Operator | Description | Example |
---|---|---|
== | Checks if the values of two operands are equal, if yes then the condition becomes true. | (A == B) is not true. |
!= | Checks if the values of two operands are equal, if values are not equal then the condition becomes true. | (A != B) is true. |
> | Checks if the value of the left operand is greater than the value of the right operand, if yes then the condition becomes true. | (A > B) is not true. |
< | Checks if the value of the left operand is less than the value of the right operand, if yes then the condition becomes true. | (A < B) is true. |
>= | Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then the condition becomes true. | (A >= B) is not true. |
<= | Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then the condition becomes true. | (A <= B) is true. |
Example
Consider the following example to understand the available relational operators in C++.
Copy and paste the following C++ program into a file named test.cpp, compile, and run the program.
Example
#include <iostream>
using namespace std;
int main()
{
int a = 21;
int b = 10;
int c;
if( a == b )
{
cout << "Line 1 - a is equal to b" << endl;
}
else
{
cout << "Line 1 - a is not equal to b" << endl;
}
// Additional relational operations can be added here
return 0;
}
When the above code is compiled and executed, it produces the following result:
Line 1 - a is not equal to b
if (a < b)
{
cout << "Line 2 - a is less than b" << endl;
}
else
{
cout << "Line 2 - a is not less than b" << endl;
}
if (a > b)
{
cout << "Line 3 - a is greater than b" << endl;
}
else
{
cout << "Line 3 - a is not greater than b" << endl;
}
/* Change the values of a and b */
a = 5;
b = 20;
if (a <= b)
{
cout << "Line 4 - a is either less than or equal to b" << endl;
}
if (b >= a)
{
cout << "Line 5 - b is either greater than or equal to a" << endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to a
Logical Operators
The following table shows the relational logical operators supported by C++.
Assume variable A holds 1 and variable B holds 0, then:
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both operands are non-zero, then the condition becomes true. | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | !(A && B) is true. |
Example
Consider the following example to understand the logical operators available in C++.
Copy and paste the following C++ program into test.cpp file, compile and run the program.
Example
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 20;
int c;
if (a && b)
{
cout << "Line 1 - Condition is true" << endl;
}
if (a || b)
{
cout << "Line 2 - Condition is true" << endl;
}
/* Change the values of a and b */
a = 0;
b = 10;
if (a && b)
{
cout << "Line 3 - Condition is true" << endl;
}
else
{
cout << "Line 4 - Condition is not true" << endl;
}
if (!(a && b))
{
cout << "Line 5 - Condition is true" << endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Line 1 - Condition is true
Line 2 - Condition is true
Line 4 - Condition is not true
Line 5 - Condition is true
Bitwise Operators
Bitwise operators work 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 |
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
A&B = 0000 1100
A\|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C++. Assume variable A holds 60 and variable B holds 13, then:
Operator | Description | Example | |||
---|---|---|---|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) will give 12, which is 0000 1100 | |||
Bitwise OR Operator, performs "OR" operation on binary bits. Operation rules: 0 | 0=0; 0 | 1=1; 1 | 0=1; 1 | 1=1; (A | B) will result in 61, which is 0011 1101. |
XOR Operator, performs "XOR" operation on binary bits. Operation rules: 0^0=0; 0^1=1; 1^0=1; 1^1=0; (A ^ B) will result in 49, which is 0011 0001.
Bitwise NOT Operator, performs "NOT" operation on binary bits. Operation rules: ~1=-2; ~0=-1; (~A) will result in -61, which is 1100 0011, the two's complement form of a signed binary number.
Left Shift Operator. Shifts all binary bits of an operand to the left by a number of positions (discarding the leftmost bits and padding with zeros on the right). A << 2 will result in 240, which is 1111 0000.
Right Shift Operator. Shifts all binary bits of a number to the right by a number of positions, padding with 0 for positive numbers and 1 for negative numbers on the left, and discarding the rightmost bits. A >> 2 will result in 15, which is 0000 1111.
Example
Please see the example below to understand the bitwise operators available in C++.
Copy and paste the following C++ program into the test.cpp file, compile, and run the program.
Example
#include <iostream>
using namespace std;
int main()
{
unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
int c = 0;
c = a & b; // 12 = 0000 1100
cout << "Line 1 - c 的值是 " << c << endl ;
c = a | b; // 61 = 0011 1101
cout << "Line 2 - c 的值是 " << c << endl ;
c = a ^ b; // 49 = 0011 0001
cout << "Line 3 - c 的值是 " << c << endl ;
c = ~a; // -61 = 1100 0011
cout << "Line 4 - c 的值是 " << c << endl ;
c = a << 2; // 240 = 1111 0000
cout << "Line 5 - c 的值是 " << c << endl ;
c = a >> 2; // 15 = 0000 1111
cout << "Line 6 - c 的值是 " << c << endl ;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Line 1 - c 的值是 12
Line 2 - c 的值是 61
Line 3 - c 的值是 49
Line 4 - c 的值是 -61
Line 5 - c 的值是 240
Line 6 - c 的值是 15
Assignment Operators
The table below lists the assignment operators supported by C++:
Operator | Description | Example |
---|---|---|
= | Simple assignment operator, assigns the value of the right operand to the left operand | C = A + B will assign the value of A + B to C |
+= | Add AND assignment operator, adds the right operand to the left operand and assigns the result to the left operand | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator, subtracts the right operand from the left operand and assigns the result to the left operand | C -= A is equivalent to C = C - A |
*= | Multiply AND assignment operator, multiplies the right operand with the left operand and assigns the result to the left operand | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator, divides the left operand by the right operand and assigns the result to the left operand | C /= A is equivalent to C = C / A |
%= | Modulus AND assignment operator, takes modulus using two operands and assigns the result to the left operand | C %= A is equivalent to C = C % A |
<<= | Left shift AND assignment operator | C <<= 2 is the same as C = C << 2 |
>>= | Right shift AND assignment operator | C >>= 2 is the same as C = C >> 2 |
&= | Bitwise AND assignment operator | C &= 2 is the same as C = C & 2 |
This is a Chinese to English translation, please provide the English translation for this text. Do not provide any explanations or text apart from the translation. Chinese: | ^= | Bitwise XOR and assignment operator | C ^= 2 is equivalent to C = C ^ 2 | | |= | Bitwise OR and assignment operator | C |= 2 is equivalent to C = C | 2 |
Example
Please refer to the following example to understand the assignment operators available in C++.
Copy and paste the following C++ program into a file named test.cpp, compile, and run the program.
Example
#include <iostream>
using namespace std;
int main()
{
int a = 21;
int c;
c = a;
cout << "Line 1 - = Operator Example, c value = : " << c << endl;
c += a;
cout << "Line 2 - += Operator Example, c value = : " << c << endl;
c -= a;
cout << "Line 3 - -= Operator Example, c value = : " << c << endl;
c *= a;
cout << "Line 4 - *= Operator Example, c value = : " << c << endl;
c /= a;
cout << "Line 5 - /= Operator Example, c value = : " << c << endl;
c = 200;
c %= a;
cout << "Line 6 - %= Operator Example, c value = : " << c << endl;
c <<= 2;
cout << "Line 7 - <<= Operator Example, c value = : " << c << endl;
c >>= 2;
cout << "Line 8 - >>= Operator Example, c value = : " << c << endl;
c &= 2;
cout << "Line 9 - &= Operator Example, c value = : " << c << endl;
c ^= 2;
cout << "Line 10 - ^= Operator Example, c value = : " << c << endl;
c |= 2;
cout << "Line 11 - |= Operator Example, c value = : " << c << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Line 1 - = Operator Example, c value = 21
Line 2 - += Operator Example, c value = 42
Line 3 - -= Operator Example, c value = 21
Line 4 - *= Operator Example, c value = 441
Line 5 - /= Operator Example, c value = 21
Line 6 - %= Operator Example, c value = 11
Line 7 - <<= Operator Example, c value = 44
Line 8 - >>= Operator Example, c value = 11
Line 9 - &= Operator Example, c value = 2
Line 10 - ^= Operator Example, c value = 0
Line 11 - |= Operator Example, c value = 2
Miscellaneous Operators
The following table lists some other important operators supported by C++.
Operator | Description |
---|---|
sizeof | sizeof Operator |
Condition ? X : Y | Conditional Operator |
, | Comma Operator |
. (dot) and -> (arrow) | Member Operators |
Cast | Casting Operators |
& | Pointer Operator & |
* | Pointer Operator * |
Operator Precedence in C++
The precedence of operators determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication and division operators have higher precedence than the addition and subtraction operators.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20, because the operator * has higher precedence than +, so it first gets multiplied with 3*2, and then adds into 7.
The following table lists the precedence of operators from highest to lowest. Operators with higher precedence appear at the top of the table, and those with lower precedence appear at the bottom. In an expression, higher precedence operators will be evaluated first.
| Category | Operators | Associativity |
Operator Precedence in C++
Category | Operators | 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 |
Example
Consider the following example to understand the operator precedence in C++.
Copy and paste the following C++ program into a file named test.cpp
, compile, and run the program.
Compare the results with and without parentheses, as this will yield different outcomes. This is because operators like () , / , * , and + have different precedence levels, and operators with higher precedence are evaluated first.
Example
#include <iostream>
using namespace std;
int main()
{
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
cout << "(a + b) * c / d is " << e << endl;
e = ((a + b) * c) / d; // (30 * 15 ) / 5
cout << "((a + b) * c) / d is " << e << endl;
e = (a + b) * (c / d); // (30) * (15/5)
cout << "(a + b) * (c / d) is " << e << endl;
e = a + (b * c) / d; // 20 + (150/5)
cout << "a + (b * c) / d is " << e << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
(a + b) * c / d is 90
((a + b) * c) / d is 90
(a + b) * (c / d) is 90
a + (b * c) / d is 50