NumPy - bitwise_and() 函数
输入数组中整数的二进制表示的相应位的按位与运算由 np.bitwise_and() 函数计算。
示例
import numpy as np print 'Binary equivalents of 13 and 17:' a,b = 13,17 print bin(a), bin(b) print '\n' print 'Bitwise AND of 13 and 17:' print np.bitwise_and(13, 17)
它的输出结果如下 −
Binary equivalents of 13 and 17: 0b1101 0b10001 Bitwise AND of 13 and 17: 1
您可以按如下方式验证输出。 考虑以下按位与真值表。
A | B | AND |
---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
1 | 1 | 0 | 1 | ||
AND | |||||
1 | 0 | 0 | 0 | 1 | |
result | 0 | 0 | 0 | 0 | 1 |