Re: Bitfields

From: Daniel Koepke (dkoepke@CALIFORNIA.COM)
Date: 09/25/97

On Thu, 25 Sep 1997, Andrew Helm wrote:

-+Don't use bitfields. The syntax for their use is pretty much the only
-+thing about them that isn't implementation defined. They may use up more
-+space than a bitvector system (depending on alignment requirements and
-+such), and accessing them requires at least as many instructions as
-+bitvectors. They may be completely portable, but bitvectors are so much

They're not completely portable, though, and their only
merit lies in the fact that they aren't limited to the
size of a variable type like bitvectors.  Hm, to take a
page from Zedd (reference to Terry Goodkind's fantasy
books; if you haven't read 'em, do so, good author and
more twisted than Jordan could ever hope to be.  I
actually liked his first book, _Wizard's_First_Rule_,
more than most of the novels in the Wheel of Time so
far), "Nothing's ever easy."

Bitfields are problematic and somewhat non-portable
(although their portability is probably rather trivially,
since I believe *most* compilers know about them), but
still problematic.  Bitvectors are limited by size.
Bitvector arrays work but are inefficient (eg., you
give too much room for some variables [what if you just
wanted 34 flags and you get stuck with having 128? although
that problem is easily solvable, but: what if you need
129 flags?]).  You see what I'm getting at?  All of
these solutions so far are functional, but none are truly
good solutions.

A good solution would be clean, without wasting memory,
and flexible.  And as of yet, no such implementation is
out there (to my knowledge).  Sucks, don't it?


  int bit0 : 1;


  int bitv;

Bitvector array:

  int bitv[4];

Stranger bitvector array:

  #define MAX_BITS(n)   (n / 8)
  unsigned char bitv2[MAX_BITS(128)];

this would give you a bit more memory efficiency for
when you need something larger than 32 but smaller than
64 (say 40 or 48 or 56).  Though handling ones *larger*
than a set maximum is still impossible, unless we're
willing to do:

  #define BITVECTOR(name, svar, n)      \
    { svar = (n) / 8;                   \
      unsigned char (name)[(svar)]; }

BTW, Off-topic Note of the Day: it's really easy to piss off
a girl's boyfriend, even if he is your friend...and even if
the comment you made was just a *joke* (they seem to lose
their sense of humor when conducting a long distance
relationship with someone you've been friends with for two
years :)  Luckily, it's long distance, so I won't be running
into him in any dark alleys (I wouldn't want to have to hurt
him or anything; beat him mercilessly with my keyboard).

And I bet you were all thinking that I still had a shred of
sanity...right up until that previous paragraph. :)

Daniel Koepke -:- -:-  [Shadowlord/Nether]

     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | |

This archive was generated by hypermail 2b30 : 12/08/00 PST