Building on Joachim's answer, how about:
uint32_t dropBits(uint32_t x, uint8_t bitsIn, uint8_t bitsOut)
{
return x / (1 << (bitsOut - bitsIn));
}
so, for instance if we call dropBits(1023, 10, 6)
to scale the maximum value of a 10-bit integer into 6 bits, it will return 1023 / (1 << 4)
which is 1023 / 16
i.e. 63, the maximum for a 6-bit value.
Of course, we can be tempted to help the compiler out since the denominator is a power of two:
return x >> (bitsOut - bitsIn);
This removes the division operator, doing it with a shift directly instead.
Note that this can only drop bits, it can't scale values into more bits.