one of the solutions - write function that checks that left n bits are all ON using bitmask tables
Code:
unsigned char check[] = { 0x01, 0x03, 0x07, ...};
int checkLeftBits(unsigned char value, size_t numberOfBits)
{
unsigned char cleardValue = value & check[numberOfBits-1]; /* clear bits that are out of interest */
return (cleardValue == check[numberOfBits-1]);
}
of course this sample lacks checks for suitable range of the second parameter which should be from 1 to CHAR_BITS
the second step will be to take the original value, shift it on X bits left and based on Y-X value - decide what bytes of the number should be passed into the function above with what number of bits too check)