Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Numpy bitwise_xor() Function



The NumPy bitwise_xor() Function which is used to perform a bitwise exclusive OR(XOR) operation on two arrays or scalars.

The XOR operation compares corresponding bits of the inputs by setting each bit in the result to 1 if the bits are different i.e., one is 1 and the other is 0 and to 0 if they are the same.

This function operates element-wise and supports broadcasting, enabling operations on arrays of different shapes by aligning them according to broadcasting rules. Its useful for binary data manipulation and can be applied to both integer arrays and scalars.

Syntax

Following is the Syntax of NumPy bitwise_xor() Function −

numpy.bitwise_xor(x1, x2, out=None, where=True, **kwargs)

Parameters

Following are the Parameters of NumPy bitwise_xor() Function −

  • x1: First input array or scalar. This can be an integer or an array-like structure.
  • x2: Second input array or scalar. Must be broadcastable to the shape of x1.
  • out(Optional): An array to which the result is written. It must have a shape that matches the broadcasted shape of x1 and x2.
  • where(Optional): A condition to determine where the operation is performed. The result is computed where this condition is True.
  • **kwargs: Additional keyword arguments for further customization.

Return Value

This function returns the array with the result of bitwise XOR operation.

Example 1

Following is the example of NumPy bitwise_xor() Function in which shows how the bitwise xor operates element-wise between two arrays −

import numpy as np

# Define two arrays
a = np.array([1, 2, 3, 4])
b = np.array([4, 3, 2, 1])

# Perform bitwise XOR operation
result = np.bitwise_xor(a, b)
print(result)

Output

Following is the output of the bitwise_xor() Function applied on two arrays −

[5 1 1 5]

Example 2

Below is the example which shiws how bitwise_xor() function performs element-wise XOR operations on multi-dimensional arrays by yielding a result array of the same shape −

import numpy as np

# Define two 2D arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[4, 3], [2, 1]])

# Perform bitwise XOR operation
result = np.bitwise_xor(a, b)

print(result)

Output

Following is the output of the bitwise_xor() Function applied on two arrays −

[[5 1]
 [1 5]]
numpy_binary_operators.htm
Advertisements