BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

Update:Please ignore (or downvote or consider; whatever's appropriate!). Sorry to have bothered you.

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


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re: [Way OT] C: Arrays of bitfields?
by pme (Monsignor) on Jan 18, 2015 at 09:16 UTC
    Hi BrowserUk,

    I think you cannot do that in C. But you can use macro to hide the messy expression:

    #define getsextet(UV, I) (( (UV) & ( 0x7f << (6*(I)) ) >> 6*(I))
    and the appropriate 'set' and 'clr':
    #define clrsextet(UV, I) ( (UV) & ~( 0x7f << (6*(I)) ) #define setsextet(UV, I, S) ( (UV) | ( ((S) & 0x7f) << (6*(I)) )
    The sextet have to be cleared before set. I haven't had time to test these macros, please use them carefully.
      But you can ... hide the messy expression:

      Indeed. Though I'll probably opt for inlined functions.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: [Way OT] C: Arrays of bitfields?
by Anonymous Monk on Jan 18, 2015 at 09:14 UTC

    All addressing in C is by the byte as the smallest unit (sizeof(char) is one by definition). You can't take address of a bitfield, or sizeof a bitfield. So, no array of bitfields. But underneath, bitfield access involves a shift-and-mask in any case, so there's no problem with explicit shifts like n = (uv >> (6*i)) & 0x3f.

    As far as tricks go, well you might use fd_set and related macros for a small array of booleans, but that could be seen as a slipshod misapplication.