Thread: Creating a bitfield array in C

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Albuquerque, NM USA
    Posts
    5

    Lightbulb Creating a bitfield array in C

    Hi, I am curious to know if there is a way that one could create a bitfield using the standard technique of creating a structure within a union, as follows:

    Code:
    typedef union {
          struct {
             unsigned b0 : 1;
             unsigned b1 : 1;
                   :
                   :
             unsigned b(n-1) : 1;
          } bits;
          unsigned int value;
      } BIT_FIELD_TYPE;
    Except, what I'd like to do is to replace all the single-bit elements in the bits structure with a single statement that creates an array of, say, 32 values. The clear advantage of this is that it could be traversed using an iterator, ...

    Code:
       main() {
          BIT_FIELD_TYPE foo;
          unsigned int i;
    
          ...
          for (i = 0; i < n; i++) {
             ... (print out foo.bits.b[i]) ...
          }
    So far, I've not figured out a way to do it, either as an array, or using a pointer to iterate through the individual bits.

    I'd love to hear if anyone has a solution to this - or even just comments telling me I'm nuts.

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No, you can't have an array of bits, as accessed by [ ]

    The best you can do is have something like
    #define BIT(x) (1<<(x))

    Then
    for ( i = 0 ; i < n ; i++ ) if ( value & BIT(i) )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by jaarestad View Post
    Hi, I am curious to know if there is a way that one could create a bitfield using the standard technique of creating a structure within a union, as follows:
    Structures are always padded to a whole number of bytes, sometimes a whole number of words (4 bytes, usually).
    So whilst you can declare a strcure with a 1 bit bitfield and then have an array of them, this won't do what you want.
    The answer everyone uses is to have an aray of plain unsinged chars, and access functions/macros which use the bitshift and logical operator.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    You need to be more specific about what you want to do. This is a terrible way to print a non-negative integer value in binary notation, for instance.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Location
    Albuquerque, NM USA
    Posts
    5
    I am trying to implement a data structure that implements a generic linear feedback shift register (LFSR) of variable length and with a separate coefficient vector (another 32-bit value that identifies which tap points in the LFSR participate in the feedback structure).

    Thank you, all, for your input. (I agree that if all I wanted to do was print out the binary representation, I'd never consider something like this).

    I've settled on this:

    Code:
    #define BIT(x,b) ((x & (1<<b)) >> b)
    This works very well for what I want.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    Code:
     struct BoxBits
      {
       unsigned int Bit1 :1;
       unsigned int Bit2 :1;
       unsigned int Bit3 :1;
       unsigned int Bit4 :1;
       unsigned int Bit5 :1;
       unsigned int Bit6 :1;
       unsigned int Bit7 :1;
       unsigned int Bit8 :1;
      };
     union placeholder
      {
       struct BoxBits Bits;
       unsigned int Word;
      };
     union placeholder Boxes[39];
    this is what i used to make 40 word<->bit structs

    if i say
    Code:
    Boxes[0].Bits.Bit1=8
    then box 0 bit 8 is 1 (so word would be 128)

    if i say
    Code:
    Boxes[39].Word =255
    then box 40 the bits would be 11111111

    that way i can send and poll in decimal, or send and poll one bit at a time

    seems though i can put box into an array, but cant get the bis into an array within the box array! i must be missing something!

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    unioning bitfields with other types is undefined behavior. c++ - Bitfield manipulation in C - Stack Overflow. see the reference to the C standard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minimizing bitfield size in memory
    By Thadius in forum C Programming
    Replies: 17
    Last Post: 07-23-2011, 01:14 AM
  2. Bitfield
    By ssharish2005 in forum C Programming
    Replies: 9
    Last Post: 10-03-2009, 03:12 PM
  3. Bitfield
    By $l4xklynx in forum C Programming
    Replies: 26
    Last Post: 12-25-2008, 08:52 AM
  4. bitfield "array"
    By bibsik in forum C++ Programming
    Replies: 6
    Last Post: 04-13-2006, 12:34 AM
  5. bitfield memory question
    By sufthingol in forum C++ Programming
    Replies: 19
    Last Post: 03-26-2005, 04:43 PM

Tags for this Thread