Search code examples
cpaddingbitsignshort

Bit Padding (C)


I have this code: (assuming short ints are 16 bits wide, and ints are 32 bits wide)

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

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


Solution

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

    Taking into acount @Amin Negm-Awad, when a variable is assigned from one type to another, and the value is representable in both types, like -5 as in int or short, there is no value nor precision change.

    This applies if a type is int, short, char, float, etc. It also applies if integer types are 2's complement or not. The size of the type makes no difference. Endian makes no difference, The value is preserved.