Numpy Bitwise and Function

PythonPythonBeginner
Practice Now

Introduction

NumPy is a popular scientific computing library that we use to perform numerical operations in Python. It has a large collection of mathematical functions that helps to perform mathematical operations with the help of the Numpy module. In this lab, we will cover the bitwise_and binary operation in the Numpy library.

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-86406{{"`Numpy Bitwise and Function`"}} python/lists -.-> lab-86406{{"`Numpy Bitwise and Function`"}} python/tuples -.-> lab-86406{{"`Numpy Bitwise and Function`"}} python/importing_modules -.-> lab-86406{{"`Numpy Bitwise and Function`"}} python/numerical_computing -.-> lab-86406{{"`Numpy Bitwise and Function`"}} python/build_in_functions -.-> lab-86406{{"`Numpy Bitwise and Function`"}} numpy/bool_idx -.-> lab-86406{{"`Numpy Bitwise and Function`"}} numpy/fancy_idx -.-> lab-86406{{"`Numpy Bitwise and Function`"}} end

Importing Numpy Module

To use the NumPy library, we need to import it. In the code snippet given below, we have imported the NumPy module.

import numpy as np

Understanding Bitwise And Operation

The bitwise_and() function calculates the bit-wise and of the underlying binary representation of the integers in the input array. The truth table of the bit-wise AND operation is given below:

A B A & B
0 0 0
0 1 0
1 0 0
1 1 1

We should keep these values in mind while performing bitwise and operation on any inputs.

Syntax of bitwise_and()

The following is the syntax of bitwise_and():

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

Parameters

  • 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 bit-wise AND result, else the out array will retain its original value.
  • dtype : optional argument is used to set the data type of the output.

Perform Bitwise And Operation on Scalars

In the example below, we will illustrate the usage of bitwise_and() function to perform a bitwise and operation on two scalar values.

num1 = 15
num2 = 20

print("The Input  number1 is :", num1)
print("The Input  number2 is :", num2)

output = np.bitwise_and(num1, num2)
print("The bitwise_and of 15 and 20 is: ", output)

The output of the above code would be:

The Input  number1 is : 15
The Input  number2 is : 20
The bitwise_and of 15 and 20 is: 4

Perform Bitwise And Operation on Arrays

In the following example, we will apply the bitwise_and() function on two arrays.

ar1 = [2, 8, 135]
ar2 = [3, 5, 115]

print("The Input array1 is : ", ar1)
print("The Input array2 is : ", ar2)

output_arr = np.bitwise_and(ar1, ar2)
print("The Output array after bitwise_and: ", output_arr)

The output of the above code would be:

The Input array1 is : [2, 8, 135]
The Input array2 is : [3, 5, 115]
The Output array after bitwise_and: [2 0 3]

Summary

In this lab, we covered the bitwise_and() function of the NumPy library to perform bitwise AND operation on two values or arrays. We also learned about the syntax and parameters of the function along with some examples.

Summary

Congratulations! You have completed the Numpy Bitwise and Function lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like