[comp.sys.amiga.misc] Bitfields in C

David.Plummer@p0.f70.n140.z1.FIDONET.ORG (David Plummer) (05/06/91)

Does anyone know how to build an easily- addressable bitfield array in 
C?  Exactly what I would like to be able to do it this:                
                                                                       
struct bits {                                                          
   unsigned long thebits[31] : 1;                                      
}                                                                      
                                                                       
which of course is impossible, since C doesn't allow arrays in the     
bitfield descriptors.                                                  
                                                                       
Even better would be                                                   
                                                                       
struct bit {                                                           
   unsigned long thebit : 1;                                           
}                                                                      
                                                                       
struct mybits {                                                        
   struct bit bitlist[32767];                                          
}                                                                      
                                                                       
Which Lattice at least does not pack into memory, but rather allocates 
at 32767 longs of data.  If you have any idea what I'm talking about,  
does anyone have any suggestions?
 
Thanks a lot,
Dave (plummerd@max.cc.uregina.ca)


--  
David Plummer - via FidoNet node 1:140/22
UUCP: ...!herald!weyr!70.0!David.Plummer
Domain: David.Plummer@p0.f70.n140.z1.FIDONET.ORG
Standard Disclaimers Apply...

jbickers@templar.actrix.gen.nz (John Bickers) (05/08/91)

Quoted from <214.2826444A@weyr.FIDONET.ORG> by David.Plummer@p0.f70.n140.z1.FIDONET.ORG (David Plummer):
> Does anyone know how to build an easily- addressable bitfield array in 
> C?  Exactly what I would like to be able to do it this:                

    Failing anything else, you could try using a macro. So allocate your
    bitfield array like this:

    ULONG   mybits[(NUM_FIELDS + 31)/32];

    #define GETBIT(x)   ( (mybits[x >> 5] & (1L << (x & 31L))) )

    Etc. You might find it nicer to use tables of masks, or functions
    instead of macros, etc.

> David Plummer - via FidoNet node 1:140/22
--
*** John Bickers, TAP, NZAmigaUG.        jbickers@templar.actrix.gen.nz ***
***         "Endless variations, make it all seem new" - Devo.          ***

Ingo.Wilken@arbi.informatik.uni-oldenburg.de (Ingo Wilken) (05/08/91)

David.Plummer@p0.f70.n140.z1.FIDONET.ORG (David Plummer) writes:
>Does anyone know how to build an easily- addressable bitfield array in 
>C?  Exactly what I would like to be able to do it this:                
>struct bits {                                                          
>   unsigned long thebits[31] : 1;                                      
>}                                                                      
>which of course is impossible, since C doesn't allow arrays in the     
>bitfield descriptors.                                                  
[...]
>does anyone have any suggestions?

What about this:
    struct bits { unsigned int thebits:31; };

If you want to set bit 10, you just do something like

    struct bits i;
    i.thebits = 0;

    i.thebits |= (1<<10);

and a check if bit 7 is set would be

    if( i.thebits & (1<<7) )
       printf("Yeah!\n");

Hope this helps.
Ingo
-- 
Ingo Wilken, CS Student, Univ.of Oldenburg, FRG | "Sie kennen das ja vom Pro-
wilken@uniol.uucp (..!uunet!unido!uniol!wilken) |  grammieren, man weiss ja
Ingo.Wilken@arbi.informatik.uni-oldenburg.de    |  nicht was die Dinger tun."
wilken@uniol.zer   IRC:Nobody   OL:ingo@faramir +--------------- Prof. Claus