Compute the bit-wise NOT of a boolean array in Numpy

NumpyServer Side ProgrammingProgramming

To compute the bit-wise NOT of a boolean array, 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([[True, False], [False,True ], [False, False]])

Displaying our array −

print("Array...\n",arr)

Get the datatype −

print("\nArray datatype...\n",arr.dtype)

Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...\n",arr.shape)

Get the number of elements of the Array −

print("\nElements in the Array...\n",arr.size)

To compute the bit-wise NOT of a boolean array, use the numpy.bitwise_not() method in Python Numpy −

print("\nResult (bit-wise NOT)...\n",np.bitwise_not(arr))

Example

import numpy as np

# Create a 2d array
arr = np.array([[True, False], [False,True ], [False, False]])

# Displaying our array
print("Array...\n",arr)

# Get the datatype
print("\nArray datatype...\n",arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# Get the number of elements of the Array
print("\nElements in the Array...\n",arr.size)

# To compute the bit-wise NOT of a boolean array, use the numpy.bitwise_not() method in Python Numpy
print("\nResult (bit-wise NOT)...\n",np.bitwise_not(arr))

Output

Array...
[[ True False]
[False True]
[False False]]

Array datatype...
bool

Array Dimensions...
2

Our Array Shape...
(3, 2)

Elements in the Array...
6

Result (bit-wise NOT)...
[[False True]
[ True False]
[ True True]]
raja
Updated on 16-Feb-2022 12:38:04

Advertisements