位填充 (C)

Bit Padding (C)

我有这段代码:(假设短整型是 16 位宽,整型是 32 位宽)

short int x = -5;
int y;
y = x;

用符号位向左填充以提高精度是否会改变负数的值?

Does padding (sign-extension) on the left with the sign bit to increase precision change the value of a negative number?

考虑到,当一个变量从一种类型分配给另一种类型时, 是可表示的在这两种类型中,如 intshort 中的 -5,没有 value 也没有精度变化。

这适用于 intshortcharfloat 等类型。它也适用于整数类型是否为 2 的补码。类型的大小没有区别。 Endian 没有区别,value 被保留。