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);<
The other answers don't handle the special case of n == 32 (shifting by greater than or equal to the type's width is UB), so here's a better answer:
n == 32
(uint32_t)(((uint64_t)1 << n) - 1)
Alternatively:
(n == 32) ? 0xFFFFFFFF : (((uint32_t)1 << n) - 1)