Re: Bitvectors

From: Daniel Koepke (dkoepke@CALIFORNIA.COM)
Date: 02/16/98


George wrote:

> I would like bitfields much more if you could do:
>
>   unsigned bits[1024] : 1;

Reasoning for this is that ": 1" specifies the width (in bits) of
the variable.  Thus, you are saying that an array of 1024
unsigned integers should fit inside 1 bit; _not_ that it should
create an array of 1024 bit-sized unsigned integers.

Note that, using your patch, you can compile CircleMUD with g++,
and thus, you can use classes to emulate this behaviour.

  class bit {
    unsigned int m_bit : 1;

    bit()
    : m_bit(0)
    { /* empty function */ }
  };

  class bitfield {
  public:
    int sizeof_bf;
    bit *bf;
  private:
    bitfield(int size)
    : sizeof_bf(size), bf(new bit[size])
    { /* empty function */ }
    int operator[](int x);
    inline void set(int x) { bf[x] = 1; }
    inline void rem(int x) { bf[x] = 0; }
  }

  int bitfield::operator[](int x)
  {
    if (x < 0 || x >= sizeof_bf) {
      log("Bitfield array overrun!");
      return -1;
    } else
      return bf[x].m_bit;
  }

Assuming that the above mailer-code is correct (and, chances are, it
isn't), you can now access a bitfield as:

  bitfield bitf(10); // construct the bitfield (dynamically)
  bitf.set(5);
  printf("The value of bit 5 of the bitfield is: %d\n", bitf[5]);

Note that this isn't exactly a traditional bitfield.  It is more of
a "dynamic" bitfield, since it is constructed dynamically; but,
using a class-based approach can allow you to almost completely
hide the fact that you are using bitfields, not bitvectors, just by
writing enough "operator" functions.  Of course, it'd probably take
a lot better design than the one I presented above.


-dak


     +------------------------------------------------------------+
     | 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