Numpy没有
uint10
类型但你可以使用
uint16
,以及用于检查溢出的位掩码。和使用
binary_rep
要获取10位二进制表示,请执行以下操作:
import numpy as np
MAX_WORD = 2**10
unused_bits = ~np.array([MAX_WORD-1], dtype="uint16") # Binary mask of the 6 unused_bits
words = np.random.randint(MAX_WORD, size=10, dtype="uint16") # Create 10 bit words
assert not np.any(words & unused_bits) # Check for overflow
for word in words:
print(word, np.binary_repr(word, width=10)) # Get 10 bit binary representation
binary_repr = "".join(np.binary_repr(word, width=10) for word in words)
print(binary_repr) # Full binary representation