Compute the bit-wise XOR of a 1D and a 2D array element-wise in Numpy

NumpyServer Side ProgrammingProgramming

To compute the bit-wise XOR of a 1D and a 2D array element-wise, use the numpy.bitwise_xor() method in Python Numpy.

Computes the bit-wise XOR 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.

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([[28, 60, 81, 55, 89, 43]])

Display the arrays −

print("Array 1...\n", arr1)
print("\nArray 2...\n", arr2)

Get the type of the arrays −

print("\nOur Array 1 type...\n", arr1.dtype)
print("\nOur Array 2 type...\n", arr2.dtype)

Get the dimensions of the Arrays −

print("\nOur Array 1 Dimensions...\n",arr1.ndim)
print("\nOur Array 2 Dimensions...\n",arr2.ndim)

Get the shape of the Arrays −

print("\nOur Array 1 Shape...\n",arr1.shape)
print("\nOur Array 2 Shape...\n",arr2.shape)

To compute the bit-wise XOR of a 1D and a 2D array element-wise, use the numpy.bitwise_xor() method −

print("\nResult...\n",np.bitwise_xor(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([[28, 60, 81, 55, 89, 43]])

# Display the arrays
print("Array 1...\n", arr1)
print("\nArray 2...\n", arr2)

# Get the type of the arrays
print("\nOur Array 1 type...\n", arr1.dtype)
print("\nOur Array 2 type...\n", arr2.dtype)

# Get the dimensions of the Arrays
print("\nOur Array 1 Dimensions...\n",arr1.ndim)
print("\nOur Array 2 Dimensions...\n",arr2.ndim)

# Get the shape of the Arrays
print("\nOur Array 1 Shape...\n",arr1.shape)
print("\nOur Array 2 Shape...\n",arr2.shape)

# To compute the bit-wise XOR of two arrays element-wise, use the numpy.bitwise_xor() method in Python Numpy
print("\nResult...\n",np.bitwise_xor(arr1, arr2))

Output

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

Array 2...
[[28 60 81 55 89 43]]

Our Array 1 type...
int64

Our Array 2 type...
int64

Our Array 1 Dimensions...
1

Our Array 2 Dimensions...
2

Our Array 1 Shape...
(6,)

Our Array 2 Shape...
(1, 6)

Result...
[[ 60 99 3 114 127 26]]
raja
Updated on 17-Feb-2022 11:11:42

Advertisements