Assignment Operators in C++
The assignment operator in the C++ programming language is used to manually assign or provide a value to any user-defined or declared variable parameter. Assignment operators help C++ users assign multiple types of values to variables in an existing program, from simple variable-value assignments to more complex programming operations that combine an assignment variable value with other arithmetic or logical operations.

Popular Assignment Operators
| Operator | Operator Name | Assignment Operators Description | Example |
| = | Assignment | It used to Assigns the value of the right operand to the left operand and display final output. | p = q |
| += | Add and assign | Here It used to Adds the right operand value to the left operand and assigns the result to the left operand with final result. | p += q |
| -= | Subtract and assign | Here It used to Subtracts the right operand value from the left operand value and assigns the result to the left operand finally. | p -= q |
| *= | Multiply and assign | Here It used to Multiplies the left operand value by the right operand value and assigns the result to the finally left operand. | p *= q |
| /= | Divide and assign | Here It used to Divides the left variable operand value by the right operand value and assigns the result to the finally left operand. | p /= q |
| %= | Modulus and assign | Here It used to Takes the modulus of the left variable operand with the right operand value and assigns the final remainder value result to the left operand. | p %= q |
| <<= | Left shift and assign | Here Left shifts operation used to the bits of the left operand by the number of positions specified by the user with right operand and assigns the result finally to the left operand. | p <<= q |
| >>= | Right shift and assign | Here Right shifts operation used to the bits of the left operand by the number of positions specified by the use right operand and assigns the finally result value to the left operand. | p >>= q |
| &= | Bitwise AND and assign | Here it Performs a bitwise AND operation on the left and right user define operands and assigns the final result to the left operand. | p &= q |
| |= | Bitwise OR and assign | Here it Performs a bitwise OR operation on the left and right operands and assigns the final result to the left operand. | p |= q |
| ^= | Bitwise XOR and assign | Here it Performs a bitwise XOR operation on the left and right operands and assigns the final output result to the left operand. | p ^= q |
| &&= | Logical AND and assign (C++11) | Here it Performs a logical AND operation on the left and right operands and assigns the finally result to the left operand. | p &&= q |
So, let’s explore all the assignment operators in C++ programming in detail.
Simple Assignment (=) operator in C++.
The = operator is an example of a basic assignment operator in any C++ program. It assigns the value of the right operand variable to the value of the left operand in the existing program.
Syntax of Simple Assignment (=).
variable = value;
Example of Simple Assignment (=).
#include <iostream>
using namespace std;
int main() {
int p = 7; // Here we assign the numeric value of 7 to the variable p
int q;
q = p; // Here we assign the value of the variable p (which is already 7) to q
cout << “original value of p = ” << p << ” \n simple assign value of q = ” << q << endl; // Result – original value of p = 7 simple assign value of q = 7
return 0;
}
Explanation of Simple Assignment (=).
- In this example, q = p, where p is the value of the variable p, which is already defined as 7, assigns the value of 7 to the variable q.
Add and Assign (+=) operator in C++.
In any C++ program, the += add and assign operator adds the value of the right operand variable to the left parameter operand, and displays the final result by assigning it to the left operand variable.
Syntax of the Add and Assign (+=) operator.
variable += value;
Example of the Add and Assign (+=) operator.
#include <iostream>
using namespace std;
int main() {
int p = 3;
p += 2; // here this expression is p = p + 2 => p = 5
cout << “result of add and assign p value is = ” << p << endl; // Result is = 5
return 0;
}
Explanation of the Add and Assign (+=) operator.
- In this example, p += 2 is equivalent to p = p + 2. Because of this, the final add and assign operator results in p = 5.
Subtract and assign (-=) operator in C++.
In any C++ program, the -= subtract and assign operator subtracts the right variable operand’s value from the value provided in the left variable operand, and assigns the final result to the left operand’s value and displays it.
Syntax of the Subtract and assign (-=) operator.
variable -= value;
Example of the Subtract and assign (-=) operator.
#include <iostream>
using namespace std;
int main() {
int p = 7;
p -= 3; // Here this expression is p = p – 3 => p = 4
cout << “here Subtract and Assign p value is = ” << p << endl; // Result p = 4
return 0;
}
Explanation of the Subtract and Assign (-=) operator.
- In this example, p -= 3 is equivalent to p = p – 3, so the final result value of p is 4.
Multiply and Assign (*=) operator in C++.
In a C++ program, the *= multiply and assign operator multiplies the left operand by the right operand value, and assigns the final result to the left operand value and displays it.
Syntax of the Multiply and Assign (*=) operator.
variable *= value;
Example of the Multiply and Assign (*=) operator.
#include <iostream>
using namespace std;
int main() {
int p = 7;
p *= 3; // Here this expression is p = p * 3 => p = 21
cout << “Multiply and Assign value of p = ” << p << endl; // Result p = 21
return 0;
}
Explanation of the Multiply and Assign (*=) operator.
- In this example, p *= 3 is equal to the expression p = p * 3. Because of this, the final result value of p becomes 21.
Divide and Assign (/=) operator in C++.
The /= divide and assign operator in C++ programs divides the user-defined variable left operand value by the right operand, and displays the final result output by assigning it to the left operand value.
Syntax of the Divide and Assign (/=) operator.
variable /= value;
Example of the Divide and Assign (/=) operator.
#include <iostream>
using namespace std;
int main() {
int p = 20;
p /= 4; // p = p / 4 => p = 5
cout << “Divide and Assign value of p = ” << p << endl; // Result p = 5
return 0;
}
Explanation of Divide and Assign (/=) operator.
- In this example, p /= 4 is equal to p = p / 4, so the final result of p is 5.
Modulus and Assign (%=) operator in C++.
In a C++ program, the %= Modulus and Assign operator finds the modulus of the left operand value by dividing it by the right variable operand, and displays the final modulus result by assigning it to the left operand value. This modulus operator displays the remainder of the division as a reminder value.
Syntax of the Modulus and Assign (%=) operator.
variable %= value;
Example of the Modulus and Assign (%=) operator.
#include <iostream>
using namespace std;
int main() {
int p = 7;
p %= 3; // p = p % 3 => p = 1 (here the remainder is 7 / 3)
cout << “Divide and Assign value of p = ” << p << endl; // Result p = 1
return 0;
}
Explanation of the Modulus and Assign (%=) operator.
- In this example, p %= 3 is the equivalent value of p = p % 3, and the remainder of the result 7 % 3 is the reminder value, which is the final output as 1.
Shift and Assign (<<= and >>=) operator in C++.
In C++ programs, the <<= and >>= left and right shift and assign operators are used to shift or move a bitwise left and right shift bit value from its current bit memory location one by one. The shift and assign operators shift the bits of the left operand to the specific numeric bit shift position indicated by the right operand, and display the final result output by assigning it to the left operand value.
Syntax of the Shift and Assign (<<= and >>=) operator.
variable <<= value; // Left Shift and Assign Expression
variable >>= value; // Right Shift and Assign Expression
Example of the Shift and Assign (<<= and >>=) operator (left shift).
#include <iostream>
using namespace std;
int main() {
int p = 7; // Here equivalent binary value – 0111
p <<= 3; // p = p << 3 => binary value – 00111000, which is 56 in decimal
cout << “Left shift and assign p value = ” << p << endl; // Result p = 56
return 0;
}
Explanation of the Left Shift and Assign (<<= and >>=) operator.
- In this example, p <<= 3 shifts the bits of p to the left by 3 positions. The final result is 56.
Example of the Shift and Assign (<<= and >>=) operator (right shift).
#include <iostream>
using namespace std;
int main() {
int p = 29; // here equivalent binary value – 0001 1101
p >>= 3; // p = p >> 3 => binary value – 0011, which is 3 in decimal
cout << “Right shift and assign p value = ” << p << endl; // Result p = 3
return 0;
}
Explanation of the Right Shift and Assign (<<= and >>=) operator.
- In this example, p >>= 3 shifts the bits of the p value 3 positions to the right, and the final result is 3.
Bitwise AND, OR, and XOR assignment (&=, |=, ^=) operators in C++.
The bitwise AND, AND, AND XOR operators in C++ programs help apply bitwise AND, bitwise OR, and bitwise XOR operations to the left operand value with a user-defined variable right operand, and display the final result by assigning it to the left operand.
Syntax of the bitwise AND, OR, and XOR assignment (&=, |=, ^=) operators.
variable &= value; // Bitwise AND and assign expression
variable |= value; // Bitwise OR and assign expression
variable ^= value; // Bitwise XOR and assign expression
Example of the (bitwise AND) operator.
#include <iostream>
using namespace std;
int main() {
int p = 9; // Here equivalent binary value – 00001001
int q = 3; // Here the equivalent binary value is 00000011
p &= q; // p = p & q => binary: 00001001 & 00000011 = which is 1 in decimal
cout << “Bitwise AND and assign p value = ” << p << endl; // Result p = 1
return 0;
}
Explanation of the (bitwise AND) operator.
- In this example, p &= q applies a bitwise AND operation to p and q, and the final AND bitwise operator result is 1 (00000001).
Example of the (bitwise OR) operator.
#include <iostream>
using namespace std;
int main() {
int p = 8; // Here the equivalent binary value is 00001000
int q = 2; // Here the equivalent binary value is 00000010
p |= q; // p = p | q => binary: 00001010, which is 10 in decimal
cout << “Bitwise OR and assign p value = ” << p << endl; // Result p = 10
return 0;
}
Explanation of the (bitwise OR) operator.
- In this example, p |= q applies a bitwise OR operation to p and q, and the final bitwise AND operator result is 10 (00001010).
Example of the (bitwise XOR) operator.
#include <iostream>
using namespace std;
int main() {
int p = 7; // Here the equivalent binary value is 00000111
int q = 4; // Here the equivalent binary value is 00000100
p ^= q; // p = p ^ q => binary: 00000011, which is 3 in decimal
cout << “Bitwise XOR and assign p value = ” << p << endl; // Result p = 3
return 0;
}
Explanation of the (bitwise XOR) operator.
- In this example, p ^= q applies a bitwise XOR operation to p and q, and the final XOR bitwise operator result is 3 (00000011).
Summary of Assignment Operators
| Operator | Assignment Operators Description | Example |
| = | Basic assignment operator used to assign basic value | p = q |
| += | Add and assign used to add value and assign the final result | p += q |
| -= | Subtract and assign used to subtract value and assign the final result | p -= q |
| *= | Multiply and assign used to multiply value and assign the final result | p *= q |
| /= | Divide and assign used to divide value and assign the final result | p /= q |
| %= | Modulus and assign used to add divide value and assign the final remainder value to the result | p %= q |
| <<= | Left shift and assign used to add specified left bit shifting by user | p <<= q |
| >>= | Right shift and assign used to add specified right bit shifting by user | p >>= q |
| &= | Bitwise AND and assign used to perform bitwise AND operation on data type value | p &= q |
| =` | Bitwise OR and assign used to perform bitwise OR operation on data type value | p |= q |
| ^= | Bitwise XOR and assign used to perform bitwise XOR operation on data type value | p ^= q |
