NumPy bitwise_xor() function
In this tutorial, we will cover the bitwise_xor
binary operation using the bitwise_xor()
function of the Numpy library.
In Numpy, the bitwise_xor()
function is mainly used to perform the bitwise XOR operation.
-
This function will calculate the bitwise XOR of two arrays element-wise.
-
The bitwise_xor()
function calculates the bitwise XOR of the underlying binary representation of the integers in the input array.
-
For the XOR operation, the bitwise_XOR()
function implements the ^
(C/Python operator).
Syntax of numpy.bitwise_xor()
:
The syntax required to use this function is as follows:
numpy.bitwise_xor(x1, x2, /, out, *, where=True, casting='same_kind', order='K', dtype, subok=True[, signature, extobj]) = <ufunc 'bitwise_xor'>
Parameters:
let us now take a look at the parameters of this function:
-
x1, x2
These two are input arrays and with this function, only integer and boolean types are handled. If x1.shape != x2.shape
, then they must be broadcastable to a common shape (and this shape will become the shape of the output).
-
out
This parameter mainly indicates a location in which the result is stored. If this parameter is provided, it must have a shape that the inputs broadcast to. If this parameter is either not provided or it is None then a freshly-allocated array is returned.
-
where
This parameter is used to indicate a condition that is broadcast over the input. At those locations where the condition is True, the out array will be set to the ufunc result, else the out array will retain its original value.
Returned Values:
This function will return a scalar if both x1 and x2 are scalars.
Example 1:
In the example below, we will illustrate the usage of bitwise_xor()
function:
import numpy as np
num1 = 15
num2= 20
print ("The Input number1 is :", num1)
print ("The Input number2 is :", num2)
output = np.bitwise_xor(num1, num2)
print ("The bitwise_xor of 15 and 20 is: ", output)
The Input number1 is: 15
The Input number2 is: 20
The bitwise_xor of 15 and 20 is: 27
Example 2:
In this example, we will use two arrays and then apply the bitwise_xor()
function to them:
import numpy as np
ar1 = [2, 8, 135]
ar2 = [3, 5, 115]
print ("The Input array1 is : ", ar1)
print ("The Input array2 is : ", ar2)
output_arr = np.bitwise_xor(ar1, ar2)
print ("The Output array after bitwise_xor: ", output_arr)
The Input array1 is : [2, 8, 135]
The Input array2 is : [3, 5, 115]
The Output array after bitwise_xor: [ 1 13 244]
Summary
In this tutorial, we covered the Numpy bitwise_xor()
function. We covered its basic syntax and parameters and then the values returned by this function along with multiple code examples.