Compute the bit-wise AND of a One Dimensional and a zero-dimensional array element-wise in Numpy


To compute the bit-wise AND of a 1D and a 0D array element-wise, use the numpy.bitwise_and() method in Python Numpy. Computes the bit-wise AND of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator &.

The 1st and 2nd parameter are the arrays, only integer and boolean types are handled. If x1.shape != x2.shape, they must be broadcastable to a common shape. 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

Creating two numpy arrays using the array() method. We have inserted elements of int type −

arr1 = np.array([32, 95, 82, 69, 38, 49])
arr2 = np.array(99)

Display the arrays −

print("Array 1...
", arr1) print("
Array 2...
", arr2)

Get the type of the arrays −

print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype)

Get the dimensions of the Arrays −

print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim)

Get the shape of the Arrays −

print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape)

To compute the bit-wise AND of a 1D and a 0D array element-wise, use the numpy.bitwise_and() method −

print("
Result...
",np.bitwise_and(arr1, arr2))

Example

import numpy as np

# Creating two numpy arrays using the array() method
# We have inserted elements of int type
arr1 = np.array([32, 95, 82, 69, 38, 49])
arr2 = np.array(99)

# Display the arrays
print("Array 1...
", arr1) print("
Array 2...
", arr2) # Get the type of the arrays print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype) # Get the dimensions of the Arrays print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim) # Get the shape of the Arrays print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape) # To compute the bit-wise AND of two arrays element-wise, use the numpy.bitwise_and() method in Python Numpy print("
Result...
",np.bitwise_and(arr1, arr2))

Output

Array 1...
[32 95 82 69 38 49]

Array 2...
99

Our Array 1 type...
int64

Our Array 2 type...
int64

Our Array 1 Dimensions...
1

Our Array 2 Dimensions...
0

Our Array 1 Shape...
(6,)

Our Array 2 Shape...
()

Result...
[32 67 66 65 34 33]

Updated on: 18-Feb-2022

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements