C ++:将64位整数与32位整数进行比较是否安全?

时间:2020-12-06 12:05:21

Assuming I have 2 variables:

假设我有2个变量:

uint64_t a = ...

uint32_t b = ...

Will comparing the integers yield the expected results, i.e. (a != b), or (b > a)?

比较整数会产生预期的结果,即(a!= b),还是(b> a)?

2 个解决方案

#1


20  

No problem. The compiler promotes the 32-bit to 64-bit before the comparison

没问题。编译器在比较之前将32位升级到64位

#2


8  

Short answer - yes. The 'smaller' is converted to bigger one before comparison.

简短的回答 - 是的。在比较之前,'较小'将转换为较大的一个。

#1


20  

No problem. The compiler promotes the 32-bit to 64-bit before the comparison

没问题。编译器在比较之前将32位升级到64位

#2


8  

Short answer - yes. The 'smaller' is converted to bigger one before comparison.

简短的回答 - 是的。在比较之前,'较小'将转换为较大的一个。