w3resource

NumPy Binary operations: invert() function

numpy.invert() function

The invert() function is used to compute bit-wise inversion, or bit-wise NOT, element-wise.

Some common applications of numpy.invert() include:
Image Processing: Inverting the bits in image data, such as creating a negative image by inverting the color channels or modifying the alpha channel of an image.
Cryptography: Applying bitwise NOT operations during encryption or decryption processes, especially in algorithms that involve bit manipulation.
Error Detection and Correction: Implementing error detection and correction algorithms that involve bitwise NOT operations on binary data.
Digital Signal Processing: Performing bitwise NOT operations on digital signals for various purposes, such as data scrambling or descrambling.

Syntax:

numpy.invert(x, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None,
subok=True[, signature, extobj ]) = <ufunc 'invert'>

Parameter:

Name Description Required /
Optional
x Only integer and boolean types are handled. Required
out A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. Optional
where Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone. Optional
**kwargs For other keyword-only arguments.

Return value:

out: [ndarray or scalar]
Result: This is a scalar if x is a scalar.

Example: Bitwise NOT operation with NumPy arrays and binary representation

import numpy as np
>>> np.invert(np.array([13], dtype=uint16))
array([65522], dtype=uint16)
>>> np.binary_repr(1, width=16)
'0000000000000001'
>>> np.binary_repr(65522, width=16)
'1111111111110010'

In the above example, the bitwise NOT operation is applied to a NumPy array containing a single unsigned 16-bit integer (13). The resulting array contains the inverted value (65522). To visualize the inversion, the numpy.binary_repr() function is used to display the binary representation of the input number (1) and the inverted output (65522), both with a fixed width of 16 bits. The input number has a binary representation of '0000000000000001', while the inverted output has a binary representation of '1111111111110010'.

Example: Bitwise NOT operation with signed integers and binary representation

import numpy as np
>>> np.invert(np.array([13], dtype=np.int8))
array([-14], dtype=int8)
>>> np.binary_repr(-14, width=8)
'11110010'

In the above code, the bitwise NOT operation is applied to a NumPy array containing a single signed 8-bit integer (13). The resulting array contains the inverted value (-14). To visualize the inversion, the numpy.binary_repr() function is used to display the binary representation of the output number (-14) with a fixed width of 8 bits. The inverted output has a binary representation of '11110010'.

Python Code Editor:

Previous: bitwise_xor()
Next: left_shift()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/numpy/binary-operations/invert.php