Compute the bit-wise NOT of an array with signed integer type in Numpy


To compute the bit-wise NOT of an array with signed integer type, 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.

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

Steps

At first, import the required library −

import numpy as np

Create a 2d array. The datatype is set using the "dtype" parameter. We have set the datatype to signed integer type. When using signed integer types the result is the two's complement of the result for the unsigned type −

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

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-wise, 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
# The datatype is set using the "dtype" parameter
# We have set the datatype to signed integer type
# When using signed integer types the result is the two's complement of the result for the unsigned type
arr = np.array([[92, 81, 98, 45], [22, 67, 54, 69 ], [69, 80, 80, 99]], dtype=np.int8)

# 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-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...
int8

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

81 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements