C++ Basic Assignment Operators


Assignment operators in C++ are used to assign values to variables. In addition to the basic assignment operator, there are several compound assignment operators that combine a basic arithmetic or bitwise operation with assignment. Below is a list of common assignment operators in C++:

1. Basic Assignment Operator (=)

The = operator assigns the value on the right to the variable on the left.

Example:

int a; a = 5; // Assigns the value 5 to variable a

2. Compound Assignment Operators

These operators perform an operation and assign the result to the left-hand operand in a more concise way.

  1. Addition Assignment (+=)
    Adds the right operand to the left operand and assigns the result to the left operand.

    int a = 5; a += 3; // Equivalent to a = a + 3; Result is 8
  2. Subtraction Assignment (-=)
    Subtracts the right operand from the left operand and assigns the result to the left operand.

    int a = 5; a -= 2; // Equivalent to a = a - 2; Result is 3
  3. Multiplication Assignment (*=)
    Multiplies the left operand by the right operand and assigns the result to the left operand.

    int a = 5; a *= 4; // Equivalent to a = a * 4; Result is 20
  4. Division Assignment (/=)
    Divides the left operand by the right operand and assigns the result to the left operand.

    int a = 10; a /= 2; // Equivalent to a = a / 2; Result is 5
  5. Modulus Assignment (%=)
    Takes the remainder of dividing the left operand by the right operand and assigns the result to the left operand.

    int a = 10; a %= 3; // Equivalent to a = a % 3; Result is 1
  6. Bitwise AND Assignment (&=)
    Performs a bitwise AND operation between the left operand and the right operand and assigns the result to the left operand.

    int a = 5; // 0101 in binary a &= 3; // Equivalent to a = a & 3; Result is 1 (0001 in binary)
  7. Bitwise OR Assignment (|=)
    Performs a bitwise OR operation between the left operand and the right operand and assigns the result to the left operand.

    int a = 5; // 0101 in binary a |= 3; // Equivalent to a = a | 3; Result is 7 (0111 in binary)
  8. Bitwise XOR Assignment (^=)
    Performs a bitwise XOR operation between the left operand and the right operand and assigns the result to the left operand.

    int a = 5; // 0101 in binary a ^= 3; // Equivalent to a = a ^ 3; Result is 6 (0110 in binary)
  9. Bitwise Left Shift Assignment (<<=)
    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.

    int a = 5; // 0101 in binary a <<= 1; // Equivalent to a = a << 1; Result is 10 (1010 in binary)
  10. Bitwise Right Shift Assignment (>>=)
    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.

    int a = 5; // 0101 in binary a >>= 1; // Equivalent to a = a >> 1; Result is 2 (0010 in binary)

Use Cases

  • Basic Assignment (=) is used in general value assignment to variables.
  • Compound Assignment Operators are used for conciseness and efficiency in coding, as they reduce redundancy by combining the operation and assignment in a single step.

These assignment operators make code shorter and often more readable, especially when multiple operations are being applied to the same variable.