NumPy Right Shift Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn about the right_shift() function in the NumPy library. This function is used to perform the right shift operation on an array-like object. The right_shift() function mainly shifts the bits in the binary representation of an operand to the right, just by the specified positions and appends an equal number of 0s from the left. The internal representation of numbers is in the binary format; thus, the right shift operation is equivalent to dividing a number by 2^x, where x is the number of bits to shift. We will cover the syntax of the right_shift() function, its parameters, and the values returned by this function along with code samples.

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-86498{{"`NumPy Right Shift Function`"}} python/lists -.-> lab-86498{{"`NumPy Right Shift Function`"}} python/tuples -.-> lab-86498{{"`NumPy Right Shift Function`"}} python/importing_modules -.-> lab-86498{{"`NumPy Right Shift Function`"}} python/numerical_computing -.-> lab-86498{{"`NumPy Right Shift Function`"}} python/build_in_functions -.-> lab-86498{{"`NumPy Right Shift Function`"}} numpy/bool_idx -.-> lab-86498{{"`NumPy Right Shift Function`"}} numpy/fancy_idx -.-> lab-86498{{"`NumPy Right Shift Function`"}} end

Importing NumPy Library

import numpy as np

NumPy is a Python library that stands for 'Numerical Python. It is used to perform operations on large and complex arrays.

Understanding the right_shift() Function

The right_shift() function is used to perform the right shift operation on an array-like object. The function parameters are as follows:

Syntax

numpy.right_shift(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
Parameter Description
x1 represents the input value and is in the form of an array.
x2 indicates the number of bits to remove at the right of x1. If x1.shape != x2.shape, then they must be broadcastable to a common shape, and that shape becomes the shape of the output.
out 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 indicates 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.

Returns:

This function will return x1, with its bits shifted x2 times to the right. If both x1 and x2 are scalars, the returned value is a scalar.

Example 1

We will illustrate the usage of the right_shift() function with scalar input value.

input_num = 40
bit_shift = 2

print("The input number is: ")
print(input_num)
print("The number of bit shift: ")
print(bit_shift)

output = np.right_shift(input_num, bit_shift)
print("After shifting 2 bits to the right: ")
print(output)

Output:

The input number is:
40
The number of bit shift:
2
After shifting 2 bits to the right:
10

Example 2

Now we will apply the right_shift() function to an input array.

input_arr = [8, 28, 55]
bit_shift = [3, 4, 2]

print("The input array is: ")
print(input_arr)
print("The number of bit shift: ")
print(bit_shift)

output = np.right_shift(input_arr, bit_shift)
print("After shifting bits to the right, the output array is: ")
print(output)

Output:

The input array is:
[8, 28, 55]
The number of bit shift :
[3, 4, 2]
After shifting bits to the right, the output array is:
[ 1 1 13]

Summary

In this lab, we learned about the right_shift() function of the NumPy library, used to perform the right shift operation on an array-like object. We covered its basic syntax, parameters, and the values returned by this function along with code samples. The right_shift() function is an essential tool for manipulating binary representations of numbers, and it can be especially useful in generating accurate computational results for complex numerical models.

Other Python Tutorials you may like