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

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...",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 a boolean array, use the numpy.bitwise_not() method in Python Numpy −

print("Result (bit-wise NOT)...",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...",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 a boolean array, use the numpy.bitwise_not() method in Python Numpy
print("Result (bit-wise NOT)...",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]]

Updated on: 16-Feb-2022

399 Views