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'

你可能感兴趣的:(人工智能,numpy,python,开发语言)