Closed vterron closed 6 years ago
Instead of (a) representing the number as a binary string, (b) zfill()'ing it and (c) accessing the third bit, do it the right way and use a bit mask. An elegant approach for a more civilized age.
Also, it happens to be 100x times faster, so we can re-shell this extra nanoseconds in the black market:
int(bin(flag_value)[2:].zfill(8)[-3]) 448 ns ± 4.85 ns
flag_value & 1<<2 41.3 ns ± 0.901 ns
Instead of (a) representing the number as a binary string, (b) zfill()'ing it and (c) accessing the third bit, do it the right way and use a bit mask. An elegant approach for a more civilized age.