两种方法:
- 直接 用符号 ”-“
- 取反 + 1
测试:
int main() {
//求一个数的相反数
int c = 5;
int d = -c;
cout << d << endl; //-5
d = (~c + 1); //也表示-5
cout << d << endl;
//最小值的相反数还是其本身
int e = INT_MIN;
cout << e << endl; //-2147483648
int f = -e;
cout << f << endl;//-2147483648
f = (~e + 1);
cout << f << endl; //-2147483648
//0 取反 + 1 溢出,结果还是0
return 0;
}