位转移,屏蔽或位字段结构体? [英] Bit Shifting, Masking or a Bit Field Struct?

查看:151
本文介绍了位转移,屏蔽或位字段结构体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来与位工作。我试图与现有的协议,它可以发送三种不同类型的消息工作。

I'm new to working with bits. I'm trying to work with an existing protocol, which can send three different types of messages.

类型1是一个16位的结构:

Type 1 is a 16-bit structure:

struct digital 
{
 unsigned int type:2;
 unsigned int highlow:1;
 unsigned int sig1:5;
 unsigned int :1;
 unsigned int sig2:7;
};

的前两个比特(型,在上述我的结构)总是1 0。第三比特,highlow,确定该信号是否是开还是关,以及SIG1 + SIG2一起限定了信号的12位的索引。该指数是由0,这总是在第7位。在这两个字节分割

The first two bits (type, in my struct above) are always 1 0 . The third bit, highlow, determines whether the signal is on or off, and sig1 + sig2 together define the 12-bit index of the signal. This index is split across the two bytes by a 0, which is always in bit 7.

2型是一个32位的结构。它有一个2位的,10位指数和一个16位的值,在位置27,23,15安培0的穿插; 7位字段结构重新presentation想是这样的:

Type 2 is a 32-bit structure. It has a 2-bit type, a 10-bit index and a 16-bit value, interspersed with 0's at positions 27, 23, 15 & 7. A bit-field struct representation would like something like this:

struct analog 
{
 unsigned int type:2;
 unsigned int val1:2;
 unsigned int :1;
 unsigned int sig1:3;
 unsigned int :1;
 unsigned int sig2:7;
 unsigned int :1;
 unsigned int val2:7;
 unsigned int :1;
 unsigned int val3:7;
};

SIG1&放大器; SIG2一起形成10位的索引。 val1中+ val2的+ VAL3一起形成10位索引处的信号的16位值。

sig1 & sig2 together form the 10-bit index. val1 + val2 + val3 together form the 16-bit value of the signal at the 10-bit index.

如果我知道如何与前两种结构的工作,我想我可以计算出三分之一。

If I understand how to work with the first two structs, I think I can figure out the third.

我的问题是,是否有分配一个值并有计划制定需要进入VAL1,将val2和VAL3位的方法吗?

My question is, is there a way to assign a single value and have the program work out the bits that need to go into val1, val2 and val3?

我读过关于位转换,位字段结构和填充用0。该结构似乎是要走的路,但我不知道如何实现它。那我见过位包装的例子都没有被拆分这些方式的值。最后,我希望能够创建一个模拟结构,分配指数(I = 252)和值(V = 32768),并用它做。

I've read about bit shifting, bit-field structs and padding with 0's. The struct seems like the way to go, but I'm not sure how to implement it. None of the examples of bit-packing that I've seen have values that are split the way these are. Ultimately, I'd like to be able to create an analog struct, assign an index (i = 252) and a value (v = 32768) and be done with it.

如果有人能提出适当的方法,或提供一个链接到一个类似的例子中,我会非常AP preciate它。如果它的事项,这code将被合并成一个更大的Objective-C的应用程序。

If someone could suggest the appropriate method or provide a link to a similar sample, I'd greatly appreciate it. If it matters, this code will be incorporated into a larger Objective-C app.

感谢。

布拉德

推荐答案

您可以用一系列的变化中, s和做或秒。我也做了10位索引部分2类型:

You can do it with a series of shifts, ands, and ors. I have done the 10-bit index part for Type 2:

unsigned int i = 252;

analog a = (analog)(((i << 16) & 0x7f0000) | (i << 17) & 0x7000000);

从本质上讲,这是什么code的作用是在感兴趣的10位移位INT I 来取值范围为16 - 25,那么 31零 - 和与掩码 0x7f0000 22位设置这样。它也转移了10位的另一个副本到17的范围 - 26,那么与位掩码的IT 0x7000000 设置位0 - 22和26 - 31到零。然后,它 S中的两个值共同创造你想要的零分隔值。

Essentially, what this code does is shift the 10 bits of interest in int i to the range 16 - 25, then it ands it with the bitmask 0x7f0000 to set bits 22 - 31 to zero. It also shifts another copy of the 10 bits to the range 17 - 26, then it ands it with the bitmask 0x7000000 to set bits 0 - 22 and 26 - 31 to zero. Then it ors the two values together to create your desired zero-separated value.

...我没有绝对的把握,我计算正确的位掩码,但我希望你有这个想法。只是移位,掩模,和或合并

.. I'm not absolutely sure that I counted the bitmasks correctly, but I hope you've got the idea. Just shift, and-mask, and or-merge.

编辑:的方法2:

analog a;
a.sig1 = (i & 0x7f); // mask out bit 8 onwards
a.sig2 = ((i<<1) & 0x700); // shift left by one, then mask out bits 0-8

在第二个想法方法2是更具可读性,所以你应该用这个。

On second thought method 2 is more readable, so you should probably use this.

这篇关于位转移,屏蔽或位字段结构体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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