The program below has a bug. Which statement correctly describes it?
#include <stdio.h>
unsigned low_mask(int bits) { /* mask with the lowest `bits` bits set */
return (1u << bits) - 1;
}
int main(void) {
printf("%u\n", low_mask(8));
printf("%u\n", low_mask(32));
return 0;
}
A shift count must be less than the width of the type. Handle 32 separately: return bits >= 32 ? UINT_MAX : (1u << bits) - 1;. On x86 the unchecked version usually returns 0, not the mask you wanted.