How to set (in most elegant way) exactly n least significant bits of uint32_t? That is to write a function void setbits(uint32_t *x, int n);<
n
uint32_t
void setbits(uint32_t *x, int n);<
If n is zero then no bits should be set based on the question.
const uint32_t masks[32] = {0x1, 0x3, 0x7, ..., 0xFFFFFFFF}; void setbits(uint32_t *x, int n) { if ( (n > 0) && (n <= 32) ) { *x |= masks[--n]; } }