user-0 提问: C的有符号整数取反

回复:
user: user-1
content: 引用 6 楼 ryder_luoluo 的回复:[quote=引用 5 楼 alittlenewbiek 的回复:][quote=引用 3 楼 poetryTang 的回复:]-x已经发生了自动类型提升，由int变为unsigned int，值为2147483648，所以-x>-y，只是打印的时候还把它当成int 验证了一下，似乎没有发生promotion #include <iostream> #include <stdlib.h> #include <utility> #include <stdio.h> #include <boost/type_index.hpp>  using boost::typeindex::type_id_with_cvr;  int main(int argc, char *argv[]) {     int x = 0x80000000;     int y = 0x80000001;     printf("type x = %s, type y = %s\n",            type_id_with_cvr<decltype(x)>().pretty_name().c_str(),            type_id_with_cvr<decltype(y)>().pretty_name().c_str());     printf("x=%d, y=%d\n", x, y);     printf("(-x)=%d, (-y)=%d\n", (-x), (-y));     printf("type x = %s, type y = %s\n",            type_id_with_cvr<decltype(-x)>().pretty_name().c_str(),            type_id_with_cvr<decltype(-y)>().pretty_name().c_str());     printf("type x = %s, type y = %s\n",            type_id_with_cvr<decltype((-x))>().pretty_name().c_str(),            type_id_with_cvr<decltype((-y))>().pretty_name().c_str());     if (x > y)         printf("x=%d > y=%d\n", x, y);     else         printf("x=%d < y=%d\n", x, y);     if ((-x) > (-y))         printf("(-x)=%d > (-y)=%d\n", (-x), (-y));     else         printf("(-x)=%d < (-y)=%d\n", (-x), (-y));     return 0; }  type x = int, type y = int x=-2147483648, y=-2147483647 (-x)=-2147483648, (-y)=2147483647 type x = int, type y = int type x = int, type y = int x=-2147483648 < y=-2147483647 (-x)=-2147483648 < (-y)=2147483647[/quote] 你这结果和之前对不上啊[/quote] 我这个回答并不能解决结果的问题，只是好奇promotion，所以试了一下。 至于结果，可能与操作系统和编译器有关吧，我并不清楚具体原因。

user: user-2
content: 引用 5 楼 alittlenewbiek 的回复:[quote=引用 3 楼 poetryTang 的回复:]-x已经发生了自动类型提升，由int变为unsigned int，值为2147483648，所以-x>-y，只是打印的时候还把它当成int 验证了一下，似乎没有发生promotion #include <iostream> #include <stdlib.h> #include <utility> #include <stdio.h> #include <boost/type_index.hpp>  using boost::typeindex::type_id_with_cvr;  int main(int argc, char *argv[]) {     int x = 0x80000000;     int y = 0x80000001;     printf("type x = %s, type y = %s\n",            type_id_with_cvr<decltype(x)>().pretty_name().c_str(),            type_id_with_cvr<decltype(y)>().pretty_name().c_str());     printf("x=%d, y=%d\n", x, y);     printf("(-x)=%d, (-y)=%d\n", (-x), (-y));     printf("type x = %s, type y = %s\n",            type_id_with_cvr<decltype(-x)>().pretty_name().c_str(),            type_id_with_cvr<decltype(-y)>().pretty_name().c_str());     printf("type x = %s, type y = %s\n",            type_id_with_cvr<decltype((-x))>().pretty_name().c_str(),            type_id_with_cvr<decltype((-y))>().pretty_name().c_str());     if (x > y)         printf("x=%d > y=%d\n", x, y);     else         printf("x=%d < y=%d\n", x, y);     if ((-x) > (-y))         printf("(-x)=%d > (-y)=%d\n", (-x), (-y));     else         printf("(-x)=%d < (-y)=%d\n", (-x), (-y));     return 0; }  type x = int, type y = int x=-2147483648, y=-2147483647 (-x)=-2147483648, (-y)=2147483647 type x = int, type y = int type x = int, type y = int x=-2147483648 < y=-2147483647 (-x)=-2147483648 < (-y)=2147483647[/quote] 你这结果和之前对不上啊

user: user-1
content: 引用 3 楼 poetryTang 的回复:-x已经发生了自动类型提升，由int变为unsigned int，值为2147483648，所以-x>-y，只是打印的时候还把它当成int 验证了一下，似乎没有发生promotion #include <iostream> #include <stdlib.h> #include <utility> #include <stdio.h> #include <boost/type_index.hpp>  using boost::typeindex::type_id_with_cvr;  int main(int argc, char *argv[]) {     int x = 0x80000000;     int y = 0x80000001;     printf("type x = %s, type y = %s\n",            type_id_with_cvr<decltype(x)>().pretty_name().c_str(),            type_id_with_cvr<decltype(y)>().pretty_name().c_str());     printf("x=%d, y=%d\n", x, y);     printf("(-x)=%d, (-y)=%d\n", (-x), (-y));     printf("type x = %s, type y = %s\n",            type_id_with_cvr<decltype(-x)>().pretty_name().c_str(),            type_id_with_cvr<decltype(-y)>().pretty_name().c_str());     printf("type x = %s, type y = %s\n",            type_id_with_cvr<decltype((-x))>().pretty_name().c_str(),            type_id_with_cvr<decltype((-y))>().pretty_name().c_str());     if (x > y)         printf("x=%d > y=%d\n", x, y);     else         printf("x=%d < y=%d\n", x, y);     if ((-x) > (-y))         printf("(-x)=%d > (-y)=%d\n", (-x), (-y));     else         printf("(-x)=%d < (-y)=%d\n", (-x), (-y));     return 0; }  type x = int, type y = int x=-2147483648, y=-2147483647 (-x)=-2147483648, (-y)=2147483647 type x = int, type y = int type x = int, type y = int x=-2147483648 < y=-2147483647 (-x)=-2147483648 < (-y)=2147483647

user: user-3
content: 编译器问题吧，我这边试跑了一下代码，结果是 x=-2147483648, y=-2147483647 (-x)=-2147483648, (-y)=2147483647 x=-2147483648 < y=-2147483647 (-x)=-2147483648 < (-y)=2147483647

user: user-4
content: -x已经发生了自动类型提升，由int变为unsigned int，值为2147483648，所以-x>-y，只是打印的时候还把它当成int

user: user-5
content: printf("(-x)=%ld < (-y)=%d\n", (-x), (-y));试下这行代码就变了

user: user-5
content: int型的最大整数就是2147483647，不存在2147483648