I am looking for a portable way to generate prefix bitmasks which have the first n bits set for 0 <= n <= 32 (or 64 or an arbitrary integer type bit width).
Examples:
prefix_bitmask(0) = 0b00000000000000000000000000000000u
prefix_bitmask(4) = 0b00000000000000000000000000001111u
prefix_bitmask(32) = 0b11111111111111111111111111111111u
There are two ways this can already work if we ignore the cases n == 0 or n == 32:
// "constructive": set only the required bits
uint32_t prefix_mask1(int i) { return (uint32_t(1) << i) - 1; }
// "destructive": shift unneeded bits out
uint32_t prefix_mask2(int i) { return ~uint32_t(0) >> (32 - i); }
prefix_mask1 fails for 32 and prefix_mask2 fails for 0, both because shifts larger than the integer type are undefined behavior (because CPUs are allowed to only use the lowest 5 bits of the shift size).
Is there a "canonical" way to solve this without branching?