小白教程
所有教程
关于
Search
172.70.127.9
172.70.127.9
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“NumPy 位运算”的源代码
本页内容
上一节:
Numpy_数组操作
下一节:
NumPy_字符串函数
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:NumPy 位运算}}[[Category:NumPy 教程|14]] = NumPy 位运算 = NumPy '''"bitwise_"''' 开头的函数是位运算函数。 NumPy 位运算包括以下几个函数: {| class="table table-striped table-hover" ! 函数 ! 描述 |- | <code>bitwise_and</code> | 对数组元素执行位与操作 |- | <code>bitwise_or</code> | 对数组元素执行位或操作 |- | <code>invert</code> | 按位取反 |- | <code>left_shift</code> | 向左移动二进制表示的位 |- | <code>right_shift</code> | 向右移动二进制表示的位 |} '''注:'''也可以使用 "&"、 "~"、 "|" 和 "^" 等操作符进行计算。 === bitwise_and === bitwise_and() 函数对数组中整数的二进制形式执行位与运算。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np print ('13 和 17 的二进制形式:') a,b = 13,17 print (bin(a), bin(b)) print ('\n') print ('13 和 17 的位与:') print (np.bitwise_and(13, 17)) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 13 和 17 的二进制形式: 0b1101 0b10001 13 和 17 的位与: 1 </sample> 以上实例可以用下表来说明: {| class="table table-striped table-hover" ! ! ! 1 ! 1 ! 0 ! 1 |- | AND | | | | | |- | | 1 | 0 | 0 | 0 | 1 |- | 运算结果 | 0 | 0 | 0 | 0 | 1 |} 位与操作运算规律如下: {| class="table table-striped table-hover" ! A ! B ! AND |- | 1 | 1 | 1 |- | 1 | 0 | 0 |- | 0 | 1 | 0 |- | 0 | 0 | 0 |} === bitwise_or === bitwise_or()函数对数组中整数的二进制形式执行位或运算。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a,b = 13,17 print ('13 和 17 的二进制形式:') print (bin(a), bin(b)) print ('13 和 17 的位或:') print (np.bitwise_or(13, 17)) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 13 和 17 的二进制形式: 0b1101 0b10001 13 和 17 的位或: 29 </sample> 以上实例可以用下表来说明: {| class="table table-striped table-hover" ! ! ! 1 ! 1 ! 0 ! 1 |- | OR | | | | | |- | | 1 | 0 | 0 | 0 | 1 |- | 运算结果 | 1 | 1 | 1 | 0 | 1 |} 位或操作运算规律如下: {| class="table table-striped table-hover" ! A ! B ! OR |- | 1 | 1 | 1 |- | 1 | 0 | 1 |- | 0 | 1 | 1 |- | 0 | 0 | 0 |} === invert === invert() 函数对数组中整数进行位取反运算,即 0 变成 1,1 变成 0。 对于有符号整数,取该二进制数的补码,然后 +1。二进制数,最高位为0表示正数,最高位为 1 表示负数。 看看 ~1 的计算步骤: * 将'''<code>1</code>'''(这里叫:原码)转二进制 = '''<code>00000001</code>''' * 按位取反 = '''<code>11111110</code>''' * 发现符号位(即最高位)为'''<code>1</code>'''(表示负数),将除符号位之外的其他数字取反 = '''<code>10000001</code>''' * 末位加1取其补码 = '''<code>10000010</code>''' * 转换回十进制 = '''<code>-2</code>''' <sample title="" desc="" lang="python" hererun="1"> import numpy as np print ('13 的位反转,其中 ndarray 的 dtype 是 uint8:') print (np.invert(np.array([13], dtype = np.uint8))) print ('\n') # 比较 13 和 242 的二进制表示,我们发现了位的反转 print ('13 的二进制表示:') print (np.binary_repr(13, width = 8)) print ('\n') print ('242 的二进制表示:') print (np.binary_repr(242, width = 8)) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 13 的位反转,其中 ndarray 的 dtype 是 uint8: [242] 13 的二进制表示: 00001101 242 的二进制表示: 11110010 </sample> === left_shift === left_shift() 函数将数组元素的二进制形式向左移动到指定位置,右侧附加相等数量的 0。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np print ('将 10 左移两位:') print (np.left_shift(10,2)) print ('\n') print ('10 的二进制表示:') print (np.binary_repr(10, width = 8)) print ('\n') print ('40 的二进制表示:') print (np.binary_repr(40, width = 8)) # '00001010' 中的两位移动到了左边,并在右边添加了两个 0。 </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 将 10 左移两位: 40 10 的二进制表示: 00001010 40 的二进制表示: 00101000 </sample> === right_shift === right_shift() 函数将数组元素的二进制形式向右移动到指定位置,左侧附加相等数量的 0。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np print ('将 40 右移两位:') print (np.right_shift(40,2)) print ('\n') print ('40 的二进制表示:') print (np.binary_repr(40, width = 8)) print ('\n') print ('10 的二进制表示:') print (np.binary_repr(10, width = 8)) # '00001010' 中的两位移动到了右边,并在左边添加了两个 0。 </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 将 40 右移两位: 10 40 的二进制表示: 00101000 10 的二进制表示: 00001010 </sample>
返回至“
NumPy 位运算
”。
上一节:
Numpy_数组操作
下一节:
NumPy_字符串函数