C# 中从恒定位宽度扩展的符号

2024-01-01

我有一个长度为 5 位的值。 4 位确定数字,第 5 位确定符号,从而保存 -16 和 +15 之间的任何值。如何在 C# 中实现从恒定位宽度扩展符号?我知道在 C 语言中,我可以使用类似以下的方法来完成此任务:

int x; // convert this from using 5 bits to a full int
int r; // resulting sign extended number goes here
struct {signed int x:5;} s;
r = s.x = x;

我怎样才能在 C# 中做类似的事情?


不太清楚你的意思,但它could简单如下:

int fiveBits = normal & 0x1f;

反之亦然:

int normal =  fiveBits < 16 ? fiveBits : fiveBits | -32;

如果您可以建议一些原始输入和所需的输出,那将会有所帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 中从恒定位宽度扩展的符号 的相关文章

随机推荐