C-bit fields provide a rather convenient method for determining fields of arbitrary width in the structure (do not pay attention to portability problems for a minute.) For example, here is a simple structure with a couple of fields and a βflagβ:
#pragma pack(push,1) struct my_chunk{ unsigned short fieldA: 16; unsigned short fieldB: 15; unsigned short fieldC: 1; }; #pragma pop()
Adding #pragma operators packs this structure into a 32-bit word (providing alignment of manipulations with my_chunk pointer pointers, for example, together with saving space).
Access to each field is syntactically very good:
struct my_chunk aChunk; aChunk.fieldA = 3; aChunk.fieldB = 2; aChunk.fieldC = 1;
An alternative way to do this without the help of language is pretty ugly and pretty much turns into assembler. for example, One solution is to have bit shift macros for each field that you want to access:
#define FIELD_A 0xFF00 #define FIELD_B 0x00FE #define FIELD_C 0x0001 #define get_field(p, f) ((*p)&f) #define set_field(p, f, v) (*p) = (v<<f) + (*p)&(~f) ... set_field(&my_chunk, FIELD_A, 12345);
.. or something like that (for more formality, take a look at this one )
So the question is, if I want to βdoβ bit fields in go, what is the best way to do this?
source share