Re: Counting elements in a bitvector?

From: Mike Stilson (
Date: 08/26/02

On Tue, Aug 27, 2002 at 12:54:22AM +0000, Mark Garringer wrote:
>>Just make sure you don't put anything extraneous in the same bitvector,
>>because that will be counted too.
>Yes. I created the bitvector with the express purpose of using it only for
>guilds (up to 32) and also defined a NUM_GUILDS (presently 18).
>Is there a reason that using NUM_GUILD vs. a static 32 would be bad?
>(Assuming that NUM_GUILD is increased when new guilds are added.)
>I also added this to the score info for my testing/debuging:

Hard-coding magic numbers generally isn't a good idea... they're hard to
track down if you change things later.  I'd rather use something like
"sizeof(GUILD_FLAGS(ch)) * 8)" or something similar to get the number of
bits in the flag.

This (I assume) is the reason most bitvectors are defined by the typedef
of bitvector_t, which gcc happily lets you redefine to an unsigned long
long int to get 64 bits instead of 32.  (FWIW, in do_gen_tog, it REALLY
should have result defined as a bitvector_t.  A minor annoyance I
remember from when I switched to 64 bits long ago).

Whether this would ever effect you is unknown, but hard-coding the "32"
is just a little harder to find than either forgetting about it with the
sizeof() or changing one define somewhere.

OTOH, if you know you'll never use more than 32 guilds, you could use
the lower 32 bits for one thing guild-related, and the upper 32 for
something else... but that sounds kinda ugly to me.


   | FAQ: |
   | Archives: |
   | Newbie List:   |

This archive was generated by hypermail 2b30 : 06/25/03 PDT