Bitmask helper: Create bitmask with a list of int and int-ranges list in C++ -


edit2: guys, sorry find annoying answer marked duplicate without checking if solves one's problem. suggested answer not fit stated problem. fixed number of variables vs. variable number of variables + variable combination of ranges of integers , integer values. if still think right direction, please provide valid solution stated problem or don't mark duplicate if can't so.

i'm trying find elegant way define bitmask. bitmask integer, , defines visibility of objects on map (32 levels, bits 0..31 define visibility on each of 32 levels).

what ideall have:

int visibilitymask = createvisibilitymask(0..12, 16, 22);

so above variadic function creates bit mask, .. needs overloaded create bitmask or'ed other values.

i guess 1 tough. impossible?

edit: sorry guys, if think answer duplicate of answer suggested, please create example of how other 1 used according needs. provides answer doesn't allow combination of range, , single int values in variable - if think easy resolve, please provide.

note: original question overloading "..", not possible. clarification on this.

no, can this:

#define bitrange(from,to)  (unsigned(-1) >> << << (sizeof(int)-to) >> (sizeof(int)-to))   int visibilitymask = bitrange(0,12)  |  1<<16  |  1<<22; 

answer comment thread (this long comments).

i write exclusively in c+11 now. complex metaprograming libraries.

i used constexpr function myself in case. despite fact i've filed several consexpr bugs gcc , clang (gcc-54648,clang-13970).

this quick , dirty code showing idea how this. , i've wrote "something this". trivial rewrite constexpr me, won't. i've wrote op, not have c++11 complier , might not know constexpr is. op not asking style guide, needed solution. if guys have better solution, please post answer.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -