PDA

View Full Version : Counting flags in a bit mask



Lykurg
24th July 2009, 08:59
Hi,

I am using a enum as a bit mask. I also use Q_DECLARE_FLAGS. Now I have the need to know how many flags are set, but I can't find a simple solution for that: will say no build in function. For now I use a simple loop which counts the flags. Just wondering if there is a "standard"/better way without a self defined function...


Thanks

QFlags::cont() would have been nice:cool:

wysota
24th July 2009, 10:53
Flags don't have to be exclusive, so any count() wouldn't work.

Here you go for some algorithms: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive