Problem: I want to generate a Bit mask (uint32_t) based on a given length. Following result should be achieved:
BIT_MASK(3) = 0x00..0111
BIT_MASK(32) = 0x111..111
The code given below is working for every length smaller than 32. If the length is 32, the left shift count is larger than the type width (overflow).
#define BIT(n) ( 1<<(n) )
#define BIT_MASK(len) ( BIT(len)-1 )
uint32_t length;
uint32_t mask = BIT_MASK(length);
Question: Is there any other efficient macro solution to generate a Bit mask, which is not including an additional if/else or typecast to avoid that error.