问题:自增自减时变量的值如何变化?
测试软件:Microsoft Visual Studio 2017
测试平台:Windows 10 (10.0.17763.0)
平台工具集:Visual Studio 2017 (v141)
测试过程
# include <iostream>
# include <bitset>
using namespace std;
int main()
{
int x = 123;
cout << " 此时x的二进制值是:" << (bitset<8>)x << ", 十进制值是:" << x << endl;
cout << endl;
int a = 0, b = 0, c = 0, d = 0;
a = x;
cout << " 把x的值赋给a,此时x和a的二进制值是:" << (bitset<8>)a << ", 十进制值是:" << a << endl;
a = ++x;
cout << " a = ++x 后" << endl;
cout << " 此时x的二进制值是:" << (bitset<8>)x << ", 十进制值是:" << x << endl;
cout << " 此时a的二进制值是:" << (bitset<8>)a << ", 十进制值是:" << a << endl;
cout << endl;
b = x;
cout << " 把x的值赋给b,此时x和b的二进制值是:" << (bitset<8>)b << ", 十进制值是:" << x << endl;
b = x++;
cout << " b = x++ 后" << endl;
cout << " 此时x的二进制值是:" << (bitset<8>)x << ", 十进制值是:" << x << endl;
cout << " 此时b的二进制值是:" << (bitset<8>)b << ", 十进制值是:" << b << endl;
cout << endl;
c = x;
cout << " 把x的值赋给c,此时x和c的二进制值是:" << (bitset<8>)c << ", 十进制值是:" << x << endl;
c = --x;
cout << " c = --x 后" << endl;
cout << " 此时x的二进制值是:" << (bitset<8>)x << ", 十进制值是:" << x << endl;
cout << " 此时c的二进制值是:" << (bitset<8>)c << ", 十进制值是:" << c << endl;
cout << endl;
d = x;
cout << " 把x的值赋给d,此时x和d的二进制值是:" << (bitset<8>)d << ", 十进制值是:" << x << endl;
d = x--;
cout << " d = x-- 后" << endl;
cout << " 此时x的二进制值是:" << (bitset<8>)x << ", 十进制值是:" << x << endl;
cout << " 此时d的二进制值是:" << (bitset<8>)d << ", 十进制值是:" << d << endl;
cout << endl;
// 实验结束
int m = 0x7FFFFFFF, n = 0;
n = m + 1;
unsigned int o = 0, p = 0xFFFFFFFF;
cout << " int 的取值范围是:" << n << " 到 " << m << endl;
cout << " 二进制表示是:" << (bitset<32>)n << endl;
cout << " 到 " << endl;
cout << " " << (bitset<32>)m << endl;
cout << endl;
cout << " unsigned的取值范围是:0 到" << p << endl;
cout << " 二进制表示是:" << (bitset<32>)o << endl;
cout << " 到 " << endl;
cout << " " << (bitset<32>)p << endl;
cout << endl;
cout << " 将x的值归0,计算 int 和 unsigned int 在 ++x 后的值" << endl;
x = 0;
m = n = 0;
o = p = 0;
m = ++x;
cout << " 此时x的二进制值是:" << (bitset<32>)x << ", 十进制值是:" << x << endl;
cout << " 此时m的二进制值是:" << (bitset<32>)m << ", 十进制值是:" << m << endl;
x = 0;
o = ++x;
cout << " 此时o的二进制值是:" << (bitset<32>)o << ", 十进制值是:" << o << endl;
cout << endl;
cout << " 将x的值归0,计算 int 和 unsigned int 在 --x 后的值" << endl;
x = 0;
n = --x;
cout << " 此时x的二进制值是:" << (bitset<32>)x << ", 十进制值是:" << x << endl;
cout << " 此时n的二进制值是:" << (bitset<32>)n << ", 十进制值是:" << n << endl;
x = 0;
p = --x;
cout << " 此时p的二进制值是:" << (bitset<32>)p << ", 十进制值是:" << p << endl;
}
此时x的二进制值是:01111011, 十进制值是:123
把x的值赋给a,此时x和a的二进制值是:01111011, 十进制值是:123
a = ++x 后
此时x的二进制值是:01111100, 十进制值是:124
此时a的二进制值是:01111100, 十进制值是:124
把x的值赋给b,此时x和b的二进制值是:01111100, 十进制值是:124
b = x++ 后
此时x的二进制值是:01111101, 十进制值是:125
此时b的二进制值是:01111100, 十进制值是:124
把x的值赋给c,此时x和c的二进制值是:01111101, 十进制值是:125
c = --x 后
此时x的二进制值是:01111100, 十进制值是:124
此时c的二进制值是:01111100, 十进制值是:124
把x的值赋给d,此时x和d的二进制值是:01111100, 十进制值是:124
d = x-- 后
此时x的二进制值是:01111011, 十进制值是:123
此时d的二进制值是:01111100, 十进制值是:124
int 的取值范围是:-2147483648 到 2147483647
二进制表示是:10000000000000000000000000000000
到
01111111111111111111111111111111
unsigned的取值范围是:0 到4294967295
二进制表示是:00000000000000000000000000000000
到
11111111111111111111111111111111
将x的值归0,计算 int 和 unsigned int 在 ++x 后的值
此时x的二进制值是:00000000000000000000000000000001, 十进制值是:1
此时m的二进制值是:00000000000000000000000000000001, 十进制值是:1
此时o的二进制值是:00000000000000000000000000000001, 十进制值是:1
将x的值归0,计算 int 和 unsigned int 在 --x 后的值
此时x的二进制值是:11111111111111111111111111111111, 十进制值是:-1
此时n的二进制值是:11111111111111111111111111111111, 十进制值是:-1
此时p的二进制值是:11111111111111111111111111111111, 十进制值是:4294967295
测试结论:
- 前置自增 / 自减(++i / --i)的计算结果:i 的值会变,整个表达式的值也会变。
- 后置自增 / 自减(i++ / i–) 的计算结果:i 的值会变,整个表达式的值不会变。
- 取值范围就像时钟,过了十二点就是一点,而不是十三点。