Here is the basic 'find' algorithm. If you are using C++ you should template it for reusability:
Code:
typdef char byte;
byte *
find( const byte data[], const byte * d_end,
const byte pattern[], const byte * p_end)
{
byte * d = data, * p = pattern, * result;
for(result = d; d != d_end; ++d)
{
if(p == p_end) return result; // found
if(*d != *(p++)) p = pattern, result = d+1; // reset
}
return NULL;
}
The location of the index to be saved can be calculated by substracting the address of the first element of the array from the returned pointer, then divide that by the sizeof the datatype (in this case, a char).
To turn the bits on and off:
Code:
typedef char bool;
void change_state(const byte * loc,
const byte * lc_end, bool state)
{
byte * first = loc, last = lc_end;
while(first != last) *(first++) = state;
}
void flip(const byte * loc,
const byte * lc_end)
{
byte state = (*loc == 0) ? 1 : 0;
change_state(loc, lc_end, state);
}
I hope that helps. Good luck.