登录在C#中的恒定比特宽度上延伸 [英] Sign extending from a constant bit width in C#

查看:191
本文介绍了登录在C#中的恒定比特宽度上延伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多数民众赞成在长度为5位的值。 4位确定的数量和第5位被保持-16和+15之间的任何值确定的符号,在那里。我怎样才能做到在C#中的恒定比特宽度上延伸的迹象?我知道C,我可以使用类似的后续做到这一点:

I have a value thats 5 bits in length. 4 bits determine the number and the 5th bit determines the sign, there by holding any value between -16 and +15. How can I accomplish sign extending from a constant bit width in C#? I know in C, I can use something like the follow to accomplish this:

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#?

How can I do something similar to this in C#?

推荐答案

这不是真的清楚你的意思,但它的可能的是简单的:

It's not really clear what you mean, but it could be as simple as:

int fiveBits = normal & 0x1f;

和反向:

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

如果你可以建议一些原有的输入和期望的输出,这将有助于。

If you could suggest some original input and desired output, that would help.

这篇关于登录在C#中的恒定比特宽度上延伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆