numpy.invert()的用法说明

def invert(x, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 
    """
    invert(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
    
    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. This ufunc implements the C/Python
    operator ``~``.
    
    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
    :math:`-2^{N-1}` to :math:`+2^{N-1}-1`.

该函数计算整数二进制表示的反码,(对于无符号整数,只是将每一位取反即可。)。

对于有符号的输入,返回它的无符号类型的二进制补码(负数在计算机系统中以补码的形式存储表示)。通过np.binary_repr()输出np.invert()返回值在计算机中的表示,可以看出,下面的有符号13,运用np.invert()的结果与无符号13的补码一致。

例子如下:

无符号正数13,np.invert()的结果为242(dtype = np.uint8),242(无符号)的二进制表示为“11110010”(width = 8)

无符号正数13,np.invert()的结果为65522(dtype = np.uint16),242(无符号)的二进制表示为“1111111111110010”(width = 16)

有符号正数13,np.invert()的结果为-14。-14的二进制表示补码为“11110010”

 We've seen that 13 is represented by ``00001101``.
    The invert or bit-wise NOT of 13 is then:
    
    >>> x = np.invert(np.array(13, dtype=np.uint8))
    >>> x
    242
    >>> np.binary_repr(x, width=8)
    '11110010'
    
    The result depends on the bit-width:
    
    >>> x = np.invert(np.array(13, dtype=np.uint16))
    >>> x
    65522
    >>> np.binary_repr(x, width=16)
    '1111111111110010'
    
    When using signed integer types the result is the two's complement of
    the result for the unsigned type:
    
    >>> np.invert(np.array([13], dtype=np.int8))
    array([-14], dtype=int8)
    >>> np.binary_repr(-14, width=8)
    '11110010'

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值