12 ms·
This looks wonky. Why are you writing a loop here? if (potentially_large_value >= 32) bit_pattern = 0; else bit_pattern = bit_pattern << potentially_la
by sirclueless 11y ago
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.
- thaumasiotes 11y agoSimple, it preserves the algorithm. Just setting to 0 is conceptually a different thing. I might also note that in the specific instance, potentially_large_value was the length of a string in memory.