Set last `n` bits in unsigned int
If you meant the least-significant n bits:
((uint32_t)1 << n) - 1
On most architectures, this won't work if n is 32, so you may have to make a special case for that:
n == 32 ? 0xffffffff : (1 << n) - 1
On a 64-bit architecture, a (probably) faster solution is to cast up then down:
(uint32_t)(((uint64_t)1 << n) - 1)
In fact, this might even be faster on a 32-bit architecture since it avoids branching.
Here's a method that doesn't require any arithmetic:
~(~0u << n)