在 C++ 中,将有符号整数值转换为无符号整数值的结果可以是两种不同的大小(例如:short int
到 unsigned long long int
或 long long int
到 unsigned char
) 由标准和平台独立定义(例如,不管符号整数如何表示)?
最佳答案
是的,该值已定义且独立于所使用的表示形式。 [转换积分]/2:
If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type).
显然,目标类型的大小很重要; long long
到 unsigned char
可能产生与 long long
到 unsigned int
不同的值。
关于c++ - C++中有符号到无符号的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33307058/