跳至内容

Assignment Operations

赋值运算

包含给定运算的表达式的值是在赋值后左操作数的值:

Assigning the value of x to the y variable                              y = x;

以下运算将算术或位运算与赋值运算结合在一起:

Adding x to the y variable                                             y += x;
Subtracting x from the y variable                                      y -= x;
Multiplying the y variable by x                                        y *= x;
Dividing the y variable by x                                           y /= x;
Reminder of division of the y variable by x                            y %= x;
Shift of the binary representation of y to the right by x bits        y >>= x;
Shift of the binary representation of y to the left by x bits         y <<= x;
AND bitwise operation of binary representations of y and x             y &= x;
OR bitwise operation of binary representations of y and x              y |= x;
Excluding OR bitwise operation of binary representations of y and x    y ^= x;

位运算只能应用于整数。当对 y 值进行向右/向左按 x 位进行逻辑移位时,会使用 x 值中最小的 5 个二进制数字,最高的数字将被丢弃,即移位到 0-31 位。

通过 %= 运算(y 值除以 x 的余数),结果符号等于被除数的符号。

赋值运算符可以在表达式中多次使用。在这种情况下,表达式的处理是从左到右进行的:

y=x=3;

首先,变量 x 将被赋值为 3,然后变量 y 将被赋值为 x 的值,即也是 3。

另请参阅

优先级规则

最后更新于