I am new to working with bits. I am trying to work with an existing protocol that can send three different types of messages.
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; };
The first two bits (the type in my structure above) are always 1 0. The third bit, high, determines whether the signal is on or off, and sig1 + sig2 together determine the 12-bit index of the signal. This index is split into two bytes at 0, which is always at bit 7.
Type 2 is a 32-bit structure. It has a 2-bit type, a 10-bit index, and a 16-bit value, in short with 0 at positions 27, 23, 15, and 7. The structural representation in the bit field should look 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 and sig2 together form a 10-bit index. val1 + val2 + val3 together form a 16-bit signal value at a 10-bit index.
If I understand how to work with the first two structures, I think I can understand the third.
My question is, is there a way to assign one value and the program will produce bits that need to be converted to val1, val2 and val3?
I read about bit offsets, bitfield strings and padding with 0. The structure looks like a way, but I'm not sure how to implement it. None of the bit packing examples that I have seen have values ββthat are separated as they are. Ultimately, I would like to be able to create an analog structure, assign an index (i = 252) and a value (v = 32768) and do with it.
If someone can suggest an appropriate method or provide a link to a similar sample, I would really appreciate it. If that matters, this code will be included in the larger Objective-C application.
Thanks.
Brad