This looks wonky. Why are you writing a loop here?
if (potentially_large_value >= 32) bit_pattern = 0;
else bit_pattern = bit_pattern << potentially_large_value;
That does the same thing in constant time, and doesn't loop 2^27 times on bad input.