前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python:Numpy库中的invert()函数的用法

Python:Numpy库中的invert()函数的用法

作者头像
用户7886150
修改2021-01-05 10:26:24
1.5K0
修改2021-01-05 10:26:24
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: Python中的numpy.absolute

Numpy库中的invert()函数的用法 

官方解释: 

 Compute bit-wise inversion, or bit-wise NOT, element-wise. Computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays. For signed integer inputs, the two’s complement is returned. In a two’s-complement system negative numbers are represented by the two’s complement of the absolute value. This is the most common method of representing signed integers on computers [1]. A N-bit two’s-complement system can represent every integer in the range 

         −

          2

           N

           −

           1

        -2^{N-1}

     −2N−1 to 

         +

          2

           N

           −

           1

         −

         1

        +2^{N-1}-1

     +2N−1−1. 

函数invert()计算输入数组中整数的二进制按位NOT结果. 也就是说 Numpy库中的bitwise_not() 和 invert()是一个函数,作用相同,只是名字不同. 验证一下发现两者其实是相等的: 

>>>np.bitwise_not is np.invert

True

下面举例来看invert函数的作用. 官网的例子,我们知道整数"13"以二进制表示为"00001101",将13进行invert()转化有 : 

>>> np.invert(np.arange([13], dtype=unit8))

array([242],dtype=unit8)

输出的是242,这个242又是什么呢? 将242转换成二进制数: 

>>> np.binary_repr(242, width=8)

'11110010'

这里np.binary_repr() 函数返回给定宽度中十进制数的二进制表示形式。 由输出结果可以发现,"242"的二进制表示“”11110010“”其实就是"00001101"的按位取否. 

输出的结果取决于bit_width 

当dtype取unit16时,结果会变得不同: 

>>> np.invert(np.array([13], dtype=uint16))

array([65522], dtype=uint16)

>>> np.binary_repr(x, width=16)

'0000000000001101'

>>> np.binary_repr(65522, width=16)

'1111111111110010'

当使用含符号整数类型"int8"时,结果是无符号类型的结果的二进制补码: 

>>> np.invert(np.array([13], dtype=int8))

array([-14], dtype=int8)

>>> np.binary_repr(-14, width=8)

'11110010'

参考资料 

【1】 numpy.invert——Numpy

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档