【发布时间】:2021-06-01 20:29:34
【问题描述】:
int main(){
int64_t a = -1;
uint32_t b = -1;
bool c = a > b;
std:: cout << c << std::endl;
return 0;
}
我的理解是b,这是一个较小的类型将转换为较大的类型a(unit32 to int64):
Comparing int with long and others
那么a是一个有符号值将被转换为一个无符号值:
基本上我们的比较会变成:
18446744073709551615 > 4294967295
但我的c 结果是false。我在这里错过了什么?
【问题讨论】:
-
基本上比较会变成
-1 > 4294967295。 -
但将
a视为无符号数字并不是-1。它的18446744073709551615,如果另一个值也是无符号的,不是有符号的值应该变成无符号的吗? -
b不是18446744073709551615(太大了,无法放入 32 位无符号整数),而是4294967295。 -
@Eljay
a是不是b的值。 -
您是否在cppinsights 中尝试过此操作,看看会发生什么?
标签: c++