Assignment operators in C++ are used to assign values to variables.
The most common assignment operator is =, but C++ also provides compound assignment operators, which combine arithmetic or bitwise operations with assignment in a single step.
These operators help make code more concise and efficient.
List of Assignment Operators in C++
Operator | Name | Description | Example |
---|---|---|---|
= | Assignment | Assigns the value of the right operand to the left operand | x = y |
+= | Add and assign | Adds right operand to left operand and assigns the result | x += y |
-= | Subtract and assign | Subtracts right operand from left operand and assigns the result | x -= y |
*= | Multiply and assign | Multiplies left operand by right operand and assigns the result | x *= y |
/= | Divide and assign | Divides left operand by right operand and assigns the result | x /= y |
%= | Modulus and assign | Takes modulus of left operand by right operand and assigns the result | x %= y |
&= | Bitwise AND and assign | Performs bitwise AND on left and right operands and assigns result | x &= y |
` | =` | Bitwise OR and assign | Performs bitwise OR on left and right operands and assigns result |
^= | Bitwise XOR and assign | Performs bitwise XOR on left and right operands and assigns result | x ^= y |
<<= | Left shift and assign | Shifts bits of left operand left by right operand and assigns | x <<= y |
>>= | Right shift and assign | Shifts bits of left operand right by right operand and assigns | x >>= y |
1. Basic Assignment Operator (=)
The = operator assigns the value of the right operand to the left operand.
#include <iostream> using namespace std; int main() { int x; x = 10; // Assign 10 to x cout << "Value of x: " << x << endl; return 0; }
Explanation:
- x = 10 assigns the value 10 to x.
Output:
Value of x: 10
2. Add and Assign (+=)
The += operator adds the right operand to the left operand and assigns the result to the left operand.
#include <iostream> using namespace std; int main() { int x = 5; x += 10; // x = x + 10 cout << "After += 10, x: " << x << endl; return 0; }
Explanation:
- x += 10 adds 10 to x, so x becomes 15.
Output:
After += 10, x: 15
3. Subtract and Assign (-=)
The -= operator subtracts the right operand from the left operand and assigns the result to the left operand.
#include <iostream> using namespace std; int main() { int x = 20; x -= 5; // x = x - 5 cout << "After -= 5, x: " << x << endl; return 0; }
Explanation:
- x -= 5 subtracts 5 from x, so x becomes 15.
Output:
After -= 5, x: 15
4. Multiply and Assign (*=)
The *= operator multiplies the left operand by the right operand and assigns the result to the left operand.
#include <iostream> using namespace std; int main() { int x = 4; x *= 5; // x = x * 5 cout << "After *= 5, x: " << x << endl; return 0; }
Explanation:
- x *= 5 multiplies x by 5, so x becomes 20.
Output:
After *= 5, x: 20
5. Divide and Assign (/=)
The /= operator divides the left operand by the right operand and assigns the result to the left operand.
#include <iostream> using namespace std; int main() { int x = 20; x /= 4; // x = x / 4 cout << "After /= 4, x: " << x << endl; return 0; }
Explanation:
- x /= 4 divides x by 4, so x becomes 5.
Output:
After /= 4, x: 5
6. Modulus and Assign (%=)
The %= operator takes the modulus of the left operand by the right operand and assigns the result to the left operand.
#include <iostream> using namespace std; int main() { int x = 10; x %= 3; // x = x % 3 cout << "After %= 3, x: " << x << endl; return 0; }
Explanation:
- x %= 3 calculates 10 % 3, which results in 1, so x becomes 1.
Output:
After %= 3, x: 1
7. Bitwise AND and Assign (&=)
The &= operator performs a bitwise AND on the left and right operands and assigns the result to the left operand.
#include <iostream> using namespace std; int main() { int x = 5; // Binary: 0101 x &= 3; // Binary: 0011 cout << "After &= 3, x: " << x << endl; // Output: 1 return 0; }
Explanation:
- x &= 3 performs 0101 & 0011, resulting in 0001, so x becomes 1.
Output:
After &= 3, x: 1
8. Bitwise OR and Assign (|=)
The |= operator performs a bitwise OR on the left and right operands and assigns the result to the left operand.
#include <iostream> using namespace std; int main() { int x = 5; // Binary: 0101 x |= 3; // Binary: 0011 cout << "After |= 3, x: " << x << endl; // Output: 7 return 0; }
Explanation:
- x |= 3 performs 0101 | 0011, resulting in 0111, so x becomes 7.
Output:
After |= 3, x: 7
9. Bitwise XOR and Assign (^=)
The ^= operator performs a bitwise XOR on the left and right operands and assigns the result to the left operand.
#include <iostream> using namespace std; int main() { int x = 5; // Binary: 0101 x ^= 3; // Binary: 0011 cout << "After ^= 3, x: " << x << endl; // Output: 6 return 0; }
Explanation:
- x ^= 3 performs 0101 ^ 0011, resulting in 0110, so x becomes 6.
Output:
After ^= 3, x: 6
10. Left Shift and Assign (<<=)
The <<= operator shifts the bits of the left operand to the left by the number of positions specified by the right operand, and assigns the result to the left operand.
#include <iostream> using namespace std; int main() { int x = 3; // Binary: 0011 x <<= 1; // Shift left by 1 position cout << "After <<= 1, x: " << x << endl; // Output: 6 return 0; }
Explanation:
- x <<= 1 shifts 0011 one position to the left, resulting in 0110, so x becomes 6.
Output:
After <<= 1, x: 6
11. Right Shift and Assign (>>=)
The >>= operator shifts the bits of the left operand to the right by the number of positions specified by the right operand, and assigns the result to the left operand.
#include <iostream> using namespace std; int main() { int x = 8; // Binary: 1000 x >>= 1; // Shift right by 1 position cout << "After >>= 1, x: " << x << endl; // Output: 4 return 0; }
Explanation:
- x >>= 1 shifts 1000 one position to the right, resulting in 0100, so x becomes 4.
Output:
After >>= 1, x: 4
12. Summary Table of Assignment Operators in C++
Operator | Description | Example | Equivalent To |
---|---|---|---|
= | Assigns right operand to left operand | x = y | x = y |
+= | Adds and assigns | x += y | x = x + y |
-= | Subtracts and assigns | x -= y | x = x – y |
*= | Multiplies and assigns | x *= y | x = x * y |
/= | Divides and assigns | x /= y | x = x / y |
%= | Modulus and assigns | x %= y | x = x % y |
&= | Bitwise AND and assigns | x &= y | x = x & y |
` | =` | Bitwise OR and assigns | `x |
^= | Bitwise XOR and assigns | x ^= y | x = x ^ y |
<<= | Left shift and assigns | x <<= y | x = x << y |
>>= | Right shift and assigns | x >>= y | x = x >> y |
Complete Example: Using Assignment Operators
This example demonstrates various assignment operators by initializing a variable and applying different compound assignment operators to it.
#include <iostream> using namespace std; int main() { int x = 10; x += 5; cout << "After += 5, x: " << x << endl; x -= 2; cout << "After -= 2, x: " << x << endl; x *= 3; cout << "After *= 3, x: " << x << endl; x /= 4; cout << "After /= 4, x: " << x << endl; x %= 3; cout << "After %= 3, x: " << x << endl; x <<= 1; cout << "After <<= 1, x: " << x << endl; x >>= 1; cout << "After >>= 1, x: " << x << endl; x &= 2; cout << "After &= 2, x: " << x << endl; x |= 1; cout << "After |= 1, x: " << x << endl; x ^= 3; cout << "After ^= 3, x: " << x << endl; return 0; }
Output:
After += 5, x: 15 After -= 2, x: 13 After *= 3, x: 39 After /= 4, x: 9 After %= 3, x: 0 After <<= 1, x: 0 After >>= 1, x: 0 After &= 2, x: 0 After |= 1, x: 1 After ^= 3, x: 2
Assignment operators in C++ simplify common operations by combining them with assignment in a single step.