numpy.bitwise_xor() – The NumPy bitwise_xor Python Function

Introduction to NumPy bitwise_xor

The bitwise XOR of two array elements is compute using the numpy.bitwise_xor() function. The bit-wise XOR of the underlying binary representation of the numbers in the input arrays is compute by this function.

Syntax:

numpy.bitwise_xor(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, ufunc ‘bitwise_xor’)

Parameters:

  • arr1: [array_like] Input array.
  • arr2: [array_like] Input array.
  • out: [ndarray, optional] A location in which the result is stored.
    • If provided, it must have a shape that the inputs broadcast to.
    • If not provided or None, a freshly-allocated array is return.
  • **kwargs: This allows you to pass keyword variable length of argument to a function. It is use when we want to handle a named argument in a function.
  • where: [array_like, optional] True value means to calculate the universal functions(ufunc) at that position, False value means to leave the value in the output alone.

Return: [ndarray or scalar] Result. This is a scalar if both x1 and x2 are scalars.

Code Examples of NumPy bitwise_xor

Example 01: When inputs are numbers

# welcome to softhunt.net
# Python program explaining
# bitwise_xor() function

import numpy as np
num1 = 5
num2 = 15

print ("Input number1 : ", num1)
print ("Input number2 : ", num2)
	
ans = np.bitwise_xor(num1, num2)
print ("bitwise_xor of 5 and 15 : ", ans)

Output:

Input number1 :  5
Input number2 :  15
bitwise_xor of 5 and 15 :  10

Example 01: When inputs are arrays

# welcome to softhunt.net
# Python program explaining
# bitwise_xor() function

import numpy as np

array1 = [3, 4, 54]
array2 = [23, 2, 3]

print ("Input array1 : ", array1)
print ("Input array2 : ", array2)
	
ans = np.bitwise_xor(array1, array2)
print ("Output array after bitwise_xor: ", ans)

Output:

Input array1 :  [3, 4, 54]
Input array2 :  [23, 2, 3]
Output array after bitwise_xor:  [20  6 53]

Example 03: When inputs are Boolean

# welcome to softhunt.net
# Python program explaining
# bitwise_xor() function

import numpy as np

bool1 = [True, False, False, True, False, True]
bool2 = [False, True, False, True, True, False]

print ("Input array1 : ", bool1)
print ("Input array2 : ", bool2)
	
ans = np.bitwise_xor(bool1, bool2)
print ("Output array after bitwise_xor: ", ans)

Output:

Input array1 :  [True, False, False, True, False, True]
Input array2 :  [False, True, False, True, True, False]
Output array after bitwise_xor:  [ True  True False False  True  True]

FAQs

What is the operator in NumPy?

A multidimensional array is the most fundamental type in NumPy. Unless you specify the number of dimensions and type. NumPy array assignments are normally kept as n-dimensional arrays with the minimal type necessary to retain the objects in order. Because NumPy performs operations element-by-element, multiplying 2D arrays with * is an element-by-element multiplication rather than a matrix multiplication. For traditional matrix multiplication, the @ operator can be use.

What is backslash used for in Python?

In Python strings, the backslash “\” is a special character, also called the “escape” character. It is use in representing certain whitespace characters: “\t” is a tab, “\n” is a new line, and “\r” is a carriage return.

Conclusion

That’s all for this article. If you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.bitwise_and() – The NumPy bitwise_and Python Function
  2. The NumPy bitwise_or Python Function

Leave a Comment