more that 32 flag array testing

Peter DUNIHO peted at microsoft.UUCP
Fri Oct 12 13:18:50 AEST 1990


In article <1990Oct8.165154.26747 at vitro.uucp>, fsb at vitro.uucp (Steve Brailsford) writes:
> problem I have with finding a good efficient, fast storage
> mechanism for storing 100 boolean flags.  Basically, I have

> I couldn't think of a way to use 100 bits worth of storage
> without it getting really hairy computationally.  If I use
> enums and test some location, locations are maxed at 32 bits.
> How could I find if the 33 bit was set without having some
> array of longs.  The problem is even more complex in the fact
> 
>     Steve Brailsford		Usenet:  uunet!media!vitro!fsb

Well, I think it would be okay to use an array of longs (or chars, if
you want to minimize wasted space).  Just define a macro like this:

#define BIT_SET(bitfield,bit)    (bitfield[bit/sizeof(*bitfield)] & \
     (0x01 << (bit % sizeof(*bitfield))))

Just off the top of my head (which often times gets me the wrong answers :) ),
I think that oughta work with a minimum of trouble to you.  It's easily
modifed to handle larger bitfields - or, more precisely, it doesn't need
to be modifed to handle larger bitfields.  The data types would be
    unsigned long    bitfield[4]    /* or */  unsigned char   bitfield[13];
and
    int bit;

This could be tweaked as you need it to be...the macro will return the value
of the bit (well, set will be non-zero, unset will be zero).  Other macros
to set the bits or do whatever else are similar.

Pete D.



More information about the Comp.lang.c mailing list