c.bear
c.bear

Reputation: 1445

bitwise manipulation via operator[]

I'm writing a simple bitset wrapper to easily and efficiently set, clear and read bits from an 8-bit integer. I would do these three operations via operator[], but I'm stucked, and honestly I'm not sure it is possible without losing performances (really important for my purposes).

#include <stdint.h>
#include <iostream>

class BitMask {
private:
    uint8_t mask;
public:
    inline void set(int bit) { mask |= 1 << bit; } // deprecated
    inline void clear(int bit) { mask &= ~(1 << bit); } // deprecated
    inline int read(int bit) { return (mask >> bit) & 1; } // deprecated
    bool operator[](int bit) const { return (mask >> bit) & 1; } // new way to read
    ??? // new way to write
    friend std::ostream& operator<<(std::ostream& os, const BitMask& bitmask) {
        for (int bit = 0; bit < 8; ++bit)
             os << ((bitmask.mask >> bit) & 1 ? "1" : "0");
        return os;
    }
};

int main() {
    BitMask bitmask1;
    bitmask1.set(3);
    bitmask1.clear(3);
    bitmask1.read(3);
    std::cout << bitmask1;

    BitMask bitmask2;
    bitmask2[3] = 1; // set
    bitmask2[3] = 0; // clear
    bitmask2[3]; // read
    std::cout << bitmask2;
}

Any idea?

Upvotes: 1

Views: 103

Answers (1)

marcinj
marcinj

Reputation: 49976

One way (the only way?) is to return a proxy object from your operator[] which will hold index for your bit, this way you will assign new value to your proxy object which will alter appropriate BitMask bit. For example see here: Vector, proxy class and dot operator in C++

As for a performance - it all depends how compiler will optimize your code, if your proxy class would have only inline methods then it should be fast.

Below is example how to fix your code:

#include <stdint.h>
#include <iostream>

class BitMask {
private:
  uint8_t mask;
public:
  inline void set(int bit) { mask |= 1 << bit; } // deprecated
  inline void clear(int bit) { mask &= ~(1 << bit); } // deprecated
  inline int read(int bit) const { return (mask >> bit) & 1; } // deprecated

  struct proxy_bit
  {
    BitMask& bitmask;
    int index;
    proxy_bit(BitMask& p_bitmask, int p_index) : bitmask(p_bitmask), index(p_index) {}
    proxy_bit& operator=(int rhs) {
      if (rhs)
        bitmask.set(index);
      else
        bitmask.clear(index);
      return *this;
    }
    operator int() {
      return bitmask.read(index);
    }
  };

  proxy_bit operator[](int bit) { return proxy_bit(*this, bit); } // new way to read
  int operator[](int bit) const { return read(bit); } // new way to read

  friend std::ostream& operator<<(std::ostream& os, const BitMask& bitmask) {
    for (int bit = 0; bit < 8; ++bit)
      os << ((bitmask.mask >> bit) & 1 ? "1" : "0");
    return os;
  }
};

int main() {
  BitMask bitmask1;
  bitmask1.set(3);
  bitmask1.clear(3);
  bitmask1.read(3);
  std::cout << bitmask1 << std::endl;

  BitMask bitmask2;
  bitmask2[3] = 1; // set
  bitmask2[3] = 0; // clear
  bitmask2[3]; // read
  std::cout << bitmask2 << std::endl;

  const BitMask bitmask3;
  if (bitmask3[3]) {}
  //bitmask3[3] = 1; // compile error - OK!
}

Upvotes: 1

Related Questions