This is one way to do it:
uint32_t makeMask (uint8_t length, uint8_t offset)
{
uint8_t i = 0;
uint32_t mask = 0;
while (i < length) mask += 1 << i++;
mask = mask << offset;
return mask;
}
This is a little idiomatic, that's why i favour the paper & pencil approach to have a clearer understanding of what's going on. Of course what this function does can become a preliminary step in the new getBits(). This new function, then, could take a structure containing the length and the offset (but a simple 2-element uint8_t array would suffice in this case).
Note: I tried this on my PC; it is untested on an arduino.