Compute the bit-wise NOT of an array element-wise in Numpy


To compute the bit-wise NOT of an array element-wise, use the numpy.bitwise_not() method in Python Numpy. Computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator ˜.

The where parameter is the condition broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

Steps

At first, import the required library −

import numpy as np

Create a 2d array −

arr = np.array([[92, 81, 98, 45], [22, 67, 54, 69 ], [69, 80, 80, 99]])

Displaying our array −

print("Array...
",arr)

Get the datatype −

print("
Array datatype...
",arr.dtype)

Get the dimensions of the Array −

print("
Array Dimensions...
",arr.ndim)

Get the shape of the Array −

print("
Our Array Shape...
",arr.shape)

Get the number of elements of the Array −

print("
Elements in the Array...
",arr.size)

To compute the bit-wise NOT of an array element-wise, use the numpy.bitwise_not() method −

print("
Result (bit-wise NOT)...
",np.bitwise_not(arr))

Example

import numpy as np

# Create a 2d array
arr = np.array([[92, 81, 98, 45], [22, 67, 54, 69 ], [69, 80, 80, 99]])

# Displaying our array
print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr.size) # To compute the bit-wise NOT of an array element-wise, use the numpy.bitwise_not() method in Python Numpy print("
Result (bit-wise NOT)...
",np.bitwise_not(arr))

Output

Array...
[[92 81 98 45]
[22 67 54 69]
[69 80 80 99]]

Array datatype...
int64

Array Dimensions...
2

Our Array Shape...
(3, 4)

Elements in the Array...
12

Result (bit-wise NOT)...
[[ -93 -82 -99 -46]
[ -23 -68 -55 -70]
[ -70 -81 -81 -100]]

Updated on: 17-Feb-2022

139 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements