整数的降级Demotion of Integers

ANSI 3.2.1.2:在值无法表示的情况下,将整数转换为较短的带符号整数的结果,或者将无符号整数转换为同等长度的带符号整数的结果ANSI 3.2.1.2 The result of converting an integer to a shorter signed integer, or the result of converting an unsigned integer to a signed integer of equal length, if the value cannot be represented

long 整数强制转换为 short 时,或者 short 转换为 char 时,将保留最低有效字节。When a long integer is cast to a short, or a short is cast to a char, the least-significant bytes are retained.

例如,此行For example, this line

short x = (short)0x12345678L;  

将值 0x5678 赋给 x,此行assigns the value 0x5678 to x, and this line

char y = (char)0x1234;  

将值 0x34 赋给 yassigns the value 0x34 to y.

当带符号变量转换无符号变量(或相反)时,位模式保持不变。When signed variables are converted to unsigned and vice versa, the bit patterns remain the same. 例如,将 -2 (0xFE) 强制转换为无符号值将生成 254(也是 0xFE)。For example, casting -2 (0xFE) to an unsigned value yields 254 (also 0xFE).

请参阅See Also

整数Integers