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.


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.