A better way to set flags I need to use some flags in my code. To make it a bit clearer, I am looking to set the flags in some structure, and I need to match the following needs:


The flag representation in the structureis hidden.
The following methods are provided (or the same idea at least): setFlag(structWithFlags *, ACTION_FLAGS ),
isFlagSet(structWithFlags *, ACTION_FLAGS) and removeFlag(structWithFlags *, ACTION_FLAGS)
The structure should not contain a "magic number" that defines the size of the flag bytes, since the number of flags may vary, when compiling for different projects.


In general, this is serving as part of a general module, used by many different modules, in many different combinations.

Currently, my method to achieve that is as follows:

projectSpecificNeeds.h:

typedef enum ACTION_FLAGS {
    ACTION_FLAG_NO_1,
    ACTION_FLAG_NO_2,
    //.. etc.
    FLAG_LAST
}ACTION_FLAGS ;


genericModuleThatServesEveryBody.c:

#include &lt;limits.h&gt;
#include "projectSpecificNeeds.h"
typedef struct structWithFlags {
    // Some irrelevant fields
    // each flag is represented by one bit in the flagByte array
    char flagBytes[FLAG_LAST / CHAR_BIT + 1];
}structWithFlags ;

structWithFlags * getStructWithFlags() {
    return calloc(sizeof(structWithFlags));
}

// Bitwise OR between the relevant flag byte and the flag
void setFlag(structWithFlags * st, ACTION_FLAGS flag) {
    st-&gt;flagBytes[flag / CHAR_BIT] |= 1 &lt;&lt; (flag % CHAR_BIT);
}

// return the AND between the relevant flag byte and the flag 
char isFlagSet(structWithFlags * st, ACTION_FLAGS flag) {
    return st-&gt;flagBytes[flag / CHAR_BIT] &amp; ( 1 &lt;&lt; (flag % CHAR_BIT));
}

// Bitwise AND between the relevant flag byte all bits except of the flag
void removeFlag(structWithFlags * st, ACTION_FLAGS flag) {
    st-&gt;flagBytes[flag / CHAR_BIT] &amp;= ~(1 &lt;&lt; (flag % CHAR_BIT));
}


It works well, and I met no problem with it until now. But I wonder whether there's some more elegant / readable yet memory saving method to implement this functionality.
How many flags do you need? If you can settle with only 32 or 64 different flags instead of a generic-length array of flags, then it would probably be better to allocate the flags in a simple unsigned long, to save your program from all the runtime calculations.

The main efficiency problem in your code is that the compiler can't know the value of the "flag" parameter, so it must calculate division and modulo in runtime to know where to set the flag.

Your code is also limited to one flag set at a time. A common way to write code like this from the caller's side is setFlag (&amp;the_struct, FLAG_THIS | FLAG_THAT);. The whole Windows API uses flags like that, with bitwise OR. If you stored the flags in an unsigned long that wouldn't be a problem, as you wouldn't need to index the flag array. Then the caller wouldn't need to call your function over and over to set several flags at the same time.

Surely it must be ok to group flags into 32/64 bit variables, related to a specific code module?