numpy将32位数字的某个位分配给一个数字?

时间:2019-01-17 15:51:23

标签: python numpy

在numpy中是否可以将32位数字的某些位分配为等于某个值?例如,我可以将位范围13:16设置为0x00010000吗?

weights_table = np.zeros((output_channels, 1, 1, 4)).astype(np.int32)
for channel in range(output_channels):
    weights_table[:, :, :, 31:16] = hex(0x00010000)

1 个答案:

答案 0 :(得分:0)

是的,您可以使用组合np.bitwise_ornp.bitwise_and来做到这一点:

import numpy as np
v = np.array([1,2,3,4,5]).astype(np.int32)

v = np.bitwise_or(v,0x00010000) # set required bits to 1
v = np.bitwise_and(v,0x0001FFFF) # set required bits to 0

有关详细信息,请参见Numpy: Binary operations