So I'm in a summer OO class and we have a test tomorrow based around this project. Basically we need to create an array that holds an unspecified amount of bits and write four functions that perform operations on this array- Set() //set bit with given index to 1, Unset() //set bit with given index to 0, Flip() // change bit (with given index) and Query() // return true if the given bit is set to 1, false otherwise.
Here's a complete description if anyone is interested: http://pastebin.com/v7BCCYjh and some sample runs: http://pastebin.com/1ijh5p7p
The problem I'm having is with the high level concept. I'm pretty sure we're meant to store byte representations of the bits in each index of the array. If that is true, then I'm completely at a loss for how to implement the functions. If anyone can give me some pointers on how to approach this (I need to have a good understanding of it by tonight because I have to write out some pseudo code for it tomorrow for a midterm) I would be much, much appreciative.
Here's my .h if it helps
// bitarray.h
//
// BitArray class declaration
#ifndef _BITARRAY_H
#define _BITARRAY_H
#include <iostream>
using namespace std;
class BitArray
{
friend ostream& operator<< (ostream& os, const BitArray& a);
friend bool operator== (const BitArray&, const BitArray&);
friend bool operator!= (const BitArray&, const BitArray&);
public:
BitArray(unsigned int n); // Construct an array that can handle n bits
BitArray(const BitArray&); // copy constructor
~BitArray(); // destructor
BitArray& operator= (const BitArray& a); // assignment operator
unsigned int Length() const; // return number of bits in bitarray
void Set (unsigned int index); // set bit with given index to 1
void Unset (unsigned int index); // set bit with given index to 0
void Flip (unsigned int index); // change bit (with given index)
bool Query (unsigned int index) const; // return true if the given bit
// is set to 1, false otherwise
private:
unsigned char* barray; // pointer to the bit array
int arraySize;
};
#endif
And my constructor:
BitArray::BitArray(unsigned int n){
int size = sizeof(char);
if(n%(8*size) != 0)
arraySize = ((n/(8*size))+1);
else
arraySize = n/(8*size);
barray = new unsigned char[arraySize];
for(int i = 0; i < arraySize; i++)
barray[i] = 0;
}