C++中有符号到无符号的转换

Signed to unsigned conversion in C++

在 C++ 中,将有符号整数值转换为无符号整数值的结果可以是两种不同的大小(例如:short intunsigned long long int,或 long long intunsigned char) 由标准和平台独立定义(例如,无论符号整数如何表示)?

是的,该值已定义且独立于所使用的表示形式。 [conv.integral]/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 longunsigned char 可能会产生与 long longunsigned int 不同的值。