Numpy Bitwise XOR Operation

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn about the Numpy bitwise_xor() function, which is mainly used to perform the bitwise XOR operation. We will cover its syntax, parameters, and multiple code examples to help you understand the function better.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/booleans -.-> lab-86410{{"`Numpy Bitwise XOR Operation`"}} python/lists -.-> lab-86410{{"`Numpy Bitwise XOR Operation`"}} python/tuples -.-> lab-86410{{"`Numpy Bitwise XOR Operation`"}} python/importing_modules -.-> lab-86410{{"`Numpy Bitwise XOR Operation`"}} python/numerical_computing -.-> lab-86410{{"`Numpy Bitwise XOR Operation`"}} python/build_in_functions -.-> lab-86410{{"`Numpy Bitwise XOR Operation`"}} numpy/bool_idx -.-> lab-86410{{"`Numpy Bitwise XOR Operation`"}} numpy/fancy_idx -.-> lab-86410{{"`Numpy Bitwise XOR Operation`"}} end

Import Numpy library

Before we start using the bitwise_xor() function, we need to import the Numpy library. We can do this using the following code:

import numpy as np

Understanding the bitwise_xor() function

The bitwise_xor() function returns the bitwise XOR of two arrays element-wise. It calculates the bitwise XOR of the underlying binary representation of the integers in the input array. This function implements the ^ (C/Python operator) for the XOR operation.

numpy.bitwise_xor(x1, x2, /, out, *, where=True, casting='same_kind', order='K', dtype, subok=True[, signature, extobj]) = <ufunc 'bitwise_xor'>

Parameters:

  • x1, x2: These two are input arrays and with this function, only integer and boolean types are handled.
  • out: Indicates a location in which the result is stored. If not provided, a freshly-allocated array is returned.
  • where: 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 usage of bitwise_xor() function

Example 1:

In this example, we will illustrate the usage of the bitwise_xor() function on two scalar values.

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)

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.

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)

Output:

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 lab, 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.

Other Python Tutorials you may like