2.8.8 Assignment Operators

D provides the binary assignment operators that are listed in the folloiwng table for modifying D variables. You can only modify D variables and arrays. Kernel data objects and constants may not be modified using the D assignment operators. The assignment operators have the same meaning as they do in ANSI C.

Table 2.11 D Assignment Operators

Operator

Description

=

Set the left-hand operand equal to the right-hand expression value.

+=

Increment the left-hand operand by the right-hand expression value

-=

Decrement the left-hand operand by the right-hand expression value.

*=

Multiply the left-hand operand by the right-hand expression value.

/=

Divide the left-hand operand by the right-hand expression value.

%=

Modulo the left-hand operand by the right-hand expression value.

|=

Bitwise OR the left-hand operand with the right-hand expression value.

&=

Bitwise AND the left-hand operand with the right-hand expression value.

^=

Bitwise XOR the left-hand operand with the right-hand expression value.

<<=

Shift the left-hand operand left by the number of bits specified by the right-hand expression value.

>>=

Shift the left-hand operand right by the number of bits specified by the right-hand expression value.


Aside from the assignment operator =, the other assignment operators are provided as shorthand for using the = operator with one of the other operators that were described earlier. For example, the expression x = x + 1 is equivalent to the expression x += 1, except that the expression x is evaluated one time. These assignment operators adhere to the same rules for operand types as the binary forms described earlier.

The result of any assignment operator is an expression equal to the new value of the left-hand expression. You can use the assignment operators or any of the operators described thus far in combination to form expressions of arbitrary complexity. You can use parentheses () to group terms in complex expressions.