Re: [Newbie] BPL14 & Bit vs Struct

From: Erwin S. Andreasen (erwin@ANDREASEN.COM)
Date: 06/22/98


On Mon, 22 Jun 1998, George wrote:

> Try bitfields, if you want something new. (unsigned this_is_a_bit : 1)

I don't think bitfields (in this case) are a good idea. Using just an
array of unsigned chars and macros to set bits within them is better.

Using a bitfield you have to, for each type of bitfield you have, write a
function to convert that field into a string, and back again.

Using just an array, you have a simple table like:

const char* affect_table[] = {
"blind",
"invis",
"somethignelse"
};

which can even be generated automatically based on macros - and you can at
the same time define an enum that contains values like AFF_BLIND etc. I
have for example, a h/affected.h file that contains lines like:

BIT(AFF,BLIND)
BIT(AFF,INVIS)

and that is all that is necessary to create an AFF_BLIND with value of 0,
and an entry in the above table.


And after you have such a table, you can convert the array of chars to a
string and back in any way you want. For example, a long while ago, I
added a function that allows much smarter editing of bitflags:


>> redit
Room flags: [no_mob indoors no_recall protected]
>> protected -indoors +dark
Toggled(off): PROTECTED, cleared: INDOORS, set: DARK.


Just typing 'protected' toggle it (and it came off). -indoors removed the
indoors flag (no matter what it was before). +dark turned it on(no matter
what it was before).



Using an array of chars has another advantage: save all flags as text..
and then you can remove old unnecessary flags whenever you want - flags
that do not exist are just ignored when you read them back.

This becomes even easier with C++ and templates.


Bitfields are useful however, whenever you want to have a few boolean
values you want to set and are desperate for space, which you do not want
to save/load (or if you don't care about portability) and do not want to
set/convert to text.

 =============================================================================
<erwin@andreasen.com>      Herlev, Denmark              UNIX System Programmer
<URL:http://www.abandoned.org/drylock/>     <*>         (not speaking for) DDE
 =============================================================================


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



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