C++ bitset implementation What are the issues/better ways to do this? I was just trying to implement this for practice and learn C++. Can anyone suggest efficient implementations of other functions such as count, size, all, none, etc?

#define NO_OF_BITS (8 * sizeof(unsigned int))

#include &lt;iostream&gt;
#include &lt;assert.h&gt;

template&lt;ssize_t n&gt;
class BitArray {
  private:
    int array[n];

  public:
    BitArray();
    void set(unsigned int);
    void reset(unsigned int);
    void flip(unsigned int);
    int test(unsigned int);
};

template&lt;ssize_t n&gt;
BitArray&lt;n&gt;::BitArray() {
  memset(array, 0, sizeof(array));
}

template&lt;ssize_t n&gt;
void BitArray&lt;n&gt;::set(unsigned int k) {
  array[k/NO_OF_BITS] |= (1U &lt;&lt; (k%NO_OF_BITS));
}

template&lt;ssize_t n&gt;
void BitArray&lt;n&gt;::reset(unsigned int k) {
  array[k/NO_OF_BITS] &amp;= ~(1U &lt;&lt; (k%NO_OF_BITS));
}

template&lt;ssize_t n&gt;
int BitArray&lt;n&gt;::test(unsigned int k) {
  return ( (array[k/NO_OF_BITS] &amp; (1U&lt;&lt;(k%NO_OF_BITS))) != 0 );
}

template&lt;ssize_t n&gt;
void BitArray&lt;n&gt;::flip(unsigned int k) {
  array[k/NO_OF_BITS] ^= (1U &lt;&lt;(k%NO_OF_BITS));
}


int main() {
  BitArray&lt;10&gt;* bs = new BitArray&lt;10&gt;();
  bs-&gt;set(100);
  assert(bs-&gt;test(100)&gt;=1);
  bs-&gt;reset(100);
  assert(bs-&gt;test(100)==0);
  bs-&gt;flip(100);
  assert(bs-&gt;test(100)&gt;=1);
}
Prefer typed constants over #defines:

NO_OF_BITS should be a const unsigned int or const size_t and should also be a private member of BitArray, since it is not used elsewhere.

Also, NO_OF_BITS is a bad name for the constant. Num of bits in what? Num of bits in an integer. So named it accordingly.

As it was already suggested, use CHAR_BIT from &lt;climits&gt;. Also, adding unsigned to the sizeof call doesn't really make a difference.

The ALL_UPPERCASE notation should be reserved for macros and macro constants, to differentiate those from actual C++ entities (the preprocessor is like a mini-language inside C++ that doesn't live by the same rules). PascalCase or even camelCase are more modern notations for typed constants.

Summing up, it could look something like this:

    class BitArray {
    private:
        static const unsigned int IntBits = CHAR_BIT * sizeof(int);
    ...
    };


Side note: constexpr would have been my choice over const if this was C++11.

Use unsigned types if a value should never be negative:

ssize_t is the "signed" variant of size_t. You are using ssize_t as the template parameter for the size of your bit array. The message you pass to readers of your code with this is that the size of a bit array could be negative. I don't think this would make any sense. Either use std::size_t or a plain unsigned int.

Make consistent use of const and bool:

BitArray&lt;n&gt;::test() does not modify the bit array, so it should be a const method. E.g.:

int test(unsigned int) const;
                       ^^^^^


The const will also go in the method definition.

Also, test() has no business returning an int. It is a true or false query, so it should return a bool.

You don't need memset to zero init an array:

C++ has always had the ability to zero initialize arrays in an object constructor. Most people are not aware of it an still use the verbose and error prone memset. Read the entry about zero initialization on cppreference. Also a related question on SO. You can rewrite your constructor like this:

template&lt;ssize_t n&gt;
BitArray&lt;n&gt;::BitArray()
    : array() 
    // zero initialized
{ }


Don't use dynamic allocations where a static will do:


BitArray&lt;10&gt;* bs = new BitArray&lt;10&gt;();



Sure that code is just a usage sample, but still, there is no real need to dynamically allocate the object. The ability to declared objects by value is one of the nicest features of C++, so use it instead of a dynamic alloc when it makes sense. 

// No need to 'new' it in this case. 
BitArray&lt;10&gt; bs;


Pointers/dynamic memory are normally only used when you need to extend the lifetime of an object beyond its scope of declaration. This is not the case in your example.

Not to mention, of course, that the object in your sample code is never deleted and thus leaks memory.