How do you construct a one-bit mask from a flag number?

left_shift moves integer bits toward more significant positions. Shifting one left by k selects zero-based bit k.

numpy.left_shift in practice

Flag numbers zero, two and five give masks one, four and thirty-two. Check the shift against the target type's width before constructing a fixed-width mask.

import numpy as np
np.left_shift(1,[0,2,5])

Result [1, 4, 32]

中文

如何由位编号构造单个状态掩码?

left_shift 将整数位向高位移动给定数量。将一左移 k 位可选择从零计数的第 k 位。

numpy.left_shift 算例

编号 0、2、5 的标记对应 1、4、32。固定宽度类型能表达的位数有限,先检查位编号与目标类型宽度。

import numpy as np
np.left_shift(1,[0,2,5])

结果 [1, 4, 32]

References / 参考来源

Keep reading / 继续阅读