Introduction
Assignment operators in C++ are used to assign values to variables. These operators not only assign values but also perform various operations like addition, subtraction, multiplication, etc., and then assign the result to the variable. Understanding assignment operators is essential for writing efficient and concise code.
List of Assignment Operators
- Assignment (
=
): Assigns the value on the right to the variable on the left. - Addition Assignment (
+=
): Adds the right operand to the left operand and assigns the result to the left operand. - Subtraction Assignment (
-=
): Subtracts the right operand from the left operand and assigns the result to the left operand. - Multiplication Assignment (
*=
): Multiplies the left operand by the right operand and assigns the result to the left operand. - Division Assignment (
/=
): Divides the left operand by the right operand and assigns the result to the left operand. - Modulus Assignment (
%=
): Takes the modulus of the left operand by the right operand and assigns the result to the left operand. - Bitwise AND Assignment (
&=
): Performs a bitwise AND operation and assigns the result to the left operand. - Bitwise OR Assignment (
|=
): Performs a bitwise OR operation and assigns the result to the left operand. - Bitwise XOR Assignment (
^=
): Performs a bitwise XOR operation and assigns the result to the left operand. - Left Shift Assignment (
<<=
): Left shifts the left operand by the number of bits specified by the right operand and assigns the result to the left operand. - Right Shift Assignment (
>>=
): Right shifts the left operand by the number of bits specified by the right operand and assigns the result to the left operand.
Assignment Operators with Examples
Assignment (=
)
The assignment operator assigns the value on the right to the variable on the left.
Example
#include <iostream>
using namespace std;
int main() {
int a;
a = 10; // Assigns 10 to a
cout << "a: " << a << endl;
return 0;
}
Output
a: 10
Addition Assignment (+=
)
The addition assignment operator adds the right operand to the left operand and assigns the result to the left operand.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5;
a += 3; // Equivalent to a = a + 3
cout << "a += 3: " << a << endl;
return 0;
}
Output
a += 3: 8
Subtraction Assignment (-=
)
The subtraction assignment operator subtracts the right operand from the left operand and assigns the result to the left operand.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5;
a -= 3; // Equivalent to a = a - 3
cout << "a -= 3: " << a << endl;
return 0;
}
Output
a -= 3: 2
Multiplication Assignment (*=
)
The multiplication assignment operator multiplies the left operand by the right operand and assigns the result to the left operand.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5;
a *= 3; // Equivalent to a = a * 3
cout << "a *= 3: " << a << endl;
return 0;
}
Output
a *= 3: 15
Division Assignment (/=
)
The division assignment operator divides the left operand by the right operand and assigns the result to the left operand.
Example
#include <iostream>
using namespace std;
int main() {
int a = 10;
a /= 2; // Equivalent to a = a / 2
cout << "a /= 2: " << a << endl;
return 0;
}
Output
a /= 2: 5
Modulus Assignment (%=
)
The modulus assignment operator takes the modulus of the left operand by the right operand and assigns the result to the left operand.
Example
#include <iostream>
using namespace std;
int main() {
int a = 10;
a %= 3; // Equivalent to a = a % 3
cout << "a %= 3: " << a << endl;
return 0;
}
Output
a %= 3: 1
Bitwise AND Assignment (&=
)
The bitwise AND assignment operator performs a bitwise AND operation and assigns the result to the left operand.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
a &= 3; // Equivalent to a = a & 3 (Binary: 0101 & 0011 = 0001)
cout << "a &= 3: " << a << endl;
return 0;
}
Output
a &= 3: 1
Bitwise OR Assignment (|=
)
The bitwise OR assignment operator performs a bitwise OR operation and assigns the result to the left operand.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
a |= 3; // Equivalent to a = a | 3 (Binary: 0101 | 0011 = 0111)
cout << "a |= 3: " << a << endl;
return 0;
}
Output
a |= 3: 7
Bitwise XOR Assignment (^=
)
The bitwise XOR assignment operator performs a bitwise XOR operation and assigns the result to the left operand.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
a ^= 3; // Equivalent to a = a ^ 3 (Binary: 0101 ^ 0011 = 0110)
cout << "a ^= 3: " << a << endl;
return 0;
}
Output
a ^= 3: 6
Left Shift Assignment (<<=
)
The left shift assignment operator shifts the bits of the left operand to the left by the number of bits specified by the right operand and assigns the result to the left operand.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
a <<= 1; // Equivalent to a = a << 1 (Binary: 0101 << 1 = 1010)
cout << "a <<= 1: " << a << endl;
return 0;
}
Output
a <<= 1: 10
Right Shift Assignment (>>=
)
The right shift assignment operator shifts the bits of the left operand to the right by the number of bits specified by the right operand and assigns the result to the left operand.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
a >>= 1; // Equivalent to a = a >> 1 (Binary: 0101 >> 1 = 0010)
cout << "a >>= 1: " << a << endl;
return 0;
}
Output
a >>= 1: 2
Conclusion
Assignment operators are essential tools in C++ for assigning values to variables and performing arithmetic and bitwise operations. This chapter covered the assignment (=
), addition assignment (+=
), subtraction assignment (-=
), multiplication assignment (*=
), division assignment (/=
), modulus assignment (%=
), bitwise AND assignment (&=
), bitwise OR assignment (|=
), bitwise XOR assignment (^=
), left shift assignment (<<=
), and right shift assignment (>>=
) operators with simple examples and their outputs. Understanding these operators will help you write more efficient and concise code. In the next chapter, we will explore miscellaneous operators in C++.