-1

how exactly would I check whether or not a given "item" in a bitset vector is out of bounds

eg:

struct bitset {
    unsigned char*vector;
    int byteSize;
    int bitSize;
};
// create a new, empty bit vector set of 'size' items
struct bitset * bitset_new(int size) {
    struct bitset * theSet;
    theSet = malloc(sizeof(struct bitset));
    theSet->vector = calloc(size, sizeof(char));
    theSet->bitSize = size;
    theSet->byteSize= ((size / 8) + 1);
    return theSet;
}
    int bitset_find(struct bitset * this, int k)
    {
        int arrayIndex = k/8;
        int indexPosition = k%8;
        unsigned int flag = 1;  // flag = 0000.....00001

        flag = flag << indexPosition;     // flag = 0000...010...000   (shifted k positions)

        if()
        {
        }

    }

What exactly should I have in my if statement to see if k is not in my vector?

realicado
  • 172
  • 7

1 Answers1

0

For checking bits from bitfields in general you would use the bitwise and & operator.

You will have to add some logic first to figure out which byte from vector you should be looking at.

This would look something like:

int bitset_find(struct bitset * this, int k)
{
    int arrayIndex = k/8;
    int indexPosition = k%8;
    unsigned int flag = 1;  // flag = 0000.....00001

    flag = flag << indexPosition;     // flag = 0000...010...000   (shifted k positions)


    // check to make sure arrayIndex is in range
    if (arrayIndex > this->byteSize){
       return false; // out of range
    }

    char vector_byte = this->vector[arrayIndex];

    return vector_byte & flag; // return if the field from flag is found in the correct byte from the vector.

}
Mobius
  • 2,871
  • 1
  • 19
  • 29