下表列出了 C++ 支持的赋值运算符:
运算符 | 描述 | 实例 |
---|---|---|
= | 简单的赋值运算符,把右边操作数的值赋给左边操作数 | C = A + B 将把 A + B 的值赋给 C |
+= | 加且赋值运算符,把右边操作数加上左边操作数的结果赋值给左边操作数 | C += A 相当于 C = C + A |
-= | 减且赋值运算符,把左边操作数减去右边操作数的结果赋值给左边操作数 | C -= A 相当于 C = C - A |
*= | 乘且赋值运算符,把右边操作数乘以左边操作数的结果赋值给左边操作数 | C *= A 相当于 C = C * A |
/= | 除且赋值运算符,把左边操作数除以右边操作数的结果赋值给左边操作数 | C /= A 相当于 C = C / A |
%= | 求模且赋值运算符,求两个操作数的模赋值给左边操作数 | C %= A 相当于 C = C % A |
<<= | 左移且赋值运算符 | C <<= 2 等同于 C = C << 2 |
= | 右移且赋值运算符 | C >>= 2 等同于 C = C >> 2 |
&= | 按位与且赋值运算符 | C &= 2 等同于 C = C & 2 |
^= | 按位异或且赋值运算符 | C ^= 2 等同于 C = C ^ 2 |
|= | 按位或且赋值运算符 | C |= 2 等同于 C = C | 2 |
例子:
#include <iostream>
using namespace std;
int main()
{
int i = 33;
int j;
cout << "Line 1: i = " << i << endl;
j = i;
cout << "Line 1: j = i ,j 的值 = : " << j << endl;
j += i;
cout << "Line 2: j += i ,j 的值 = : " << j << endl;
j -= i;
cout << "Line 3: j -= i ,j 的值 = : " << j << endl;
j *= i;
cout << "Line 4: j *= i ,j 的值 = : " << j << endl;
j /= i;
cout << "Line 5: j /= i ,j 的值 = : " << j << endl;
j = 232;
j %= i;
cout << "Line 1: j = " << j << endl;
cout << "Line 6: j %= i ,j 的值 = : " << j << endl;
j >>= 2;
cout << "Line 7: j >>= 2 ,j 的值 = : " << j << endl;
j <<= 2;
cout << "Line 8: j <<= 2; ,j 的值 = : " << j << endl;
j &= 2;
cout << "Line 9: j &= 2 ,j 的值 = : " << j << endl;
j ^= 2;
cout << "Line 10: j ^= 2 ,j 的值 = : " << j << endl;
j |= 2;
cout << "Line 11: j |= 2 ,j 的值 = : " << j << endl;
system("pause");
return EXIT_SUCCESS;
}
输出:
Line 1: i = 33
Line 1: j = i ,j 的值 = : 33
Line 2: j += i ,j 的值 = : 66
Line 3: j -= i ,j 的值 = : 33
Line 4: j *= i ,j 的值 = : 1089
Line 5: j /= i ,j 的值 = : 33
Line 1: j = 1
Line 6: j %= i ,j 的值 = : 1
Line 7: j >>= 2 ,j 的值 = : 0
Line 8: j <<= 2; ,j 的值 = : 0
Line 9: j &= 2 ,j 的值 = : 0
Line 10: j ^= 2 ,j 的值 = : 2
Line 11: j |= 2 ,j 的值 = : 2
请按任意键继续. . .
标签:操作数,33,左边,运算符,83,Line,赋值
From: https://www.cnblogs.com/codemagiciant/p/17369282.html