Question: 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);`. Function should handle each `n` from `0` to `32`.

Especially value `n==32` should be handled.

Answer: 
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:

```
(uint32_t)(((uint64_t)1 << n) - 1)

```

Alternatively:

```
(n == 32) ? 0xFFFFFFFF : (((uint32_t)1 << n) - 1)

```
```
((((1 << (n - 1)) - 1) << 1) | 1)

```

Set last n bits. n must be > 0. Work with n = 32.