Assignment Operators: =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, and |=

expression assignment-operator expression 
assignment-operator : one of
   =   *=   /=   %=   +=   –=   <<=   >>=   &=   ^=   |=

Remarks

Assignment operators store a value in the object designated by the left operand. There are two kinds of assignment operations: simple assignment, in which the value of the second operand is stored in the object specified by the first operand, and compound assignment, in which an arithmetic, shift, or bitwise operation is performed prior to storing the result. All assignment operators in the following table except the = operator are compound assignment operators.

Assignment Operators

Operator

Meaning

=

Store the value of the second operand in the object specified by the first operand (simple assignment).

*=

Multiply the value of the first operand by the value of the second operand; store the result in the object specified by the first operand.

/=

Divide the value of the first operand by the value of the second operand; store the result in the object specified by the first operand.

%=

Take modulus of the first operand specified by the value of the second operand; store the result in the object specified by the first operand.

+=

Add the value of the second operand to the value of the first operand; store the result in the object specified by the first operand.

–=

Subtract the value of the second operand from the value of the first operand; store the result in the object specified by the first operand.

<<=

Shift the value of the first operand left the number of bits specified by the value of the second operand; store the result in the object specified by the first operand.

>>=

Shift the value of the first operand right the number of bits specified by the value of the second operand; store the result in the object specified by the first operand.

&=

Obtain the bitwise AND of the first and second operands; store the result in the object specified by the first operand.

^=

Obtain the bitwise exclusive OR of the first and second operands; store the result in the object specified by the first operand.

|=

Obtain the bitwise inclusive OR of the first and second operands; store the result in the object specified by the first operand.

Operator Keywords

Three of the compound assignment operators have text equivalents. They are:

Operator

Equivalent

&=

and_eq

|=

or_eq

^=

xor_eq

There are two ways to access these operator keywords in your programs: include the header file iso646.h, or compile with the /Za (Disable language extensions) compiler option.

Example

// expre_Assignment_Operators.cpp
// compile with: /EHsc
// Demonstrate assignment operators
#include <iostream>
using namespace std;
int main() {
   int a = 3, b = 6, c = 10, d = 0xAAAA, e = 0x5555;

   a += b;      // a is 9
   b %= a;      // b is 6
   c >>= 1;      // c is 5
   d |= e;      // Bitwise--d is 0xFFFF 

   cout  << "a = 3, b = 6, c = 10, d = 0xAAAA, e = 0x5555" << endl
         << "a += b yields " << a << endl
         << "b %= a yields " << b << endl
         << "c >>= 1 yields " << c << endl
         << "d |= e yields " << hex << d << endl;
}

See Also

Reference

Expressions with Binary Operators

C++ Operators

Operator Precedence and Associativity

Result of Assignment Operators

Simple Assignment (C++)

Compound Assignment

C Assignment Operators