BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
Okay. That's what comes from trying to program when I've just got up!.
Somewhere in my head I had this vague recollection of seeing an 'array of bits' in what I remembered as being C code.
Now I've woken up a bit; what I was thinking of was LLVM's arbitrary bit integer type.
Sorry for the OT post, but there are people here I trust to give me good answers. This is for a Perl extension.
Writing a I::C function, I need to process the bits of a 64-bit UV in groups of 6-bits; so I've typedef a pointer to a struct of bitfields:
typedef struct { U64 _10 :4, _9 :6; _8 :6, _7 :6, _6 :6, _5 :6, _4 :6, _3 :6, _2 :6 +, _1 :6, _0 :6; } LEVELS;
Then I can reference any group of bits:((LEVELS*)&uv)->_3.
Which is all well and good, but it means writing an unwound loop to process the 11 fields, which is really inconvenient as all the other variables in the repeated sections of code are indexed.
Is there any C trick to allow the creation of 'array of bitfields'?
Or will I have to fall back to the messy: n = ( uv & ( 0x3f << (6*i) ) >> 6*i; (And will that work for the final 4-bit field?)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: [Way OT] C: Arrays of bitfields?
by pme (Monsignor) on Jan 18, 2015 at 09:16 UTC | |
by BrowserUk (Patriarch) on Jan 18, 2015 at 09:39 UTC | |
|
Re: [Way OT] C: Arrays of bitfields?
by Anonymous Monk on Jan 18, 2015 at 09:14 UTC |