python - numpy Boolean array representation of an integer -
what's easiest way produce numpy boolean array representation of integer? example, map 6
np.array([false, true, true], dtype=np.bool)
.
if n
integer, expression (n & (1 << np.arange(int(floor(log(n, 2) + 1))))) > 0
create boolean array representing bits, least significant bit in first position.
for example,
in [224]: n = 5 in [225]: math import floor, log in [226]: n = 5 in [227]: (n & (1 << np.arange(int(floor(log(n, 2) + 1))))) > 0 out[227]: array([ true, false, true], dtype=bool) in [228]: n = 8 in [229]: (n & (1 << np.arange(int(floor(log(n, 2) + 1))))) > 0 out[229]: array([false, false, false, true], dtype=bool) in [230]: n = 514 in [231]: (n & (1 << np.arange(int(floor(log(n, 2) + 1))))) > 0 out[231]: array([false, true, false, false, false, false, false, false, false, true], dtype=bool)