# Compound assignment operators

The compound assignment operators consist of a binary operator and the simple assignment operator. They perform the operation of the binary operator on both operands and store the result of that operation into the left operand, which must be a modifiable lvalue.

The following table shows the operand types of compound assignment expressions:

Operator | Left operand | Right operand |
---|---|---|

+= or -= | Arithmetic | Arithmetic |

+= or -= | Pointer | Integral type |

*=, /=, and %= | Arithmetic | Arithmetic |

<<=, >>=, &=, ^=, and |= | Integral type | Integral type |

`a *= b + c`

is
equivalent to `a = a * (b + c)`

and *not*

`a = a * b + c`

The following table lists the compound assignment operators and shows an expression using each operator:

Operator | Example | Equivalent expression |
---|---|---|

+= | `index += 2` |
```
index = index +
2
``` |

-= | ```
*(pointer++) -=
1
``` |
```
*pointer = *(pointer++)
- 1
``` |

*= | `bonus *= increase` |
```
bonus = bonus *
increase
``` |

/= | `time /= hours` |
`time = time / hours` |

%= | `allowance %= 1000` |
```
allowance = allowance
% 1000
``` |

<<= | ```
result <<=
num
``` |
```
result = result <<
num
``` |

>>= | `form >>= 1` |
```
form = form >>
1
``` |

&= | `mask &= 2` |
```
mask = mask &
2
``` |

^= | `test ^= pre_test` |
`test = test ^ pre_test` |

|= | `flag |= ON` |
`flag = flag | ON` |

Although the equivalent expression column shows the left operands (from the example column) twice, it is in effect evaluated only once.

In addition to the table of operand types, an expression is implicitly converted to the cv-unqualified type of the left operand if it is not of class type. However, if the left operand is of class type, the class becomes complete, and assignment to objects of the class behaves as a copy assignment operation. Compound expressions and conditional expressions are lvalues in C++, which allows them to be a left operand in a compound assignment expression.