简体   繁体   English

位填充(C)

[英]Bit Padding (C)

I have this code: (assuming short ints are 16 bits wide, and ints are 32 bits wide) 我有以下代码:(假设short ints为16位宽,而ints为32位宽)

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? 用符号位在左侧填充以提高精度是否会更改负数的值?

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. 考虑到@Amin Negm-Awad ,当将变量从一种类型分配给另一种类型, 并且 在两种类型中都可表示时,例如intshort -5,则没有值,也没有精度变化。

This applies if a type is int , short , char , float , etc. It also applies if integer types are 2's complement or not. 如果类型为intshortcharfloat等,则适用此方法。如果整数类型为2的补码,则也适用。 The size of the type makes no difference. 类型的大小没有区别。 Endian makes no difference, The value is preserved. 字节序没有区别,该被保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM