Numpy Left Shift Function

PythonPythonBeginner
Practice Now

Introduction

The left_shift() function is a binary operation of the NumPy library, which performs the left shift operation on the bits of an integer. This tutorial will guide you through the basic syntax, parameters, and the returned values of left_shift() function. It will also include a few examples of using the function.

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/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/booleans -.-> lab-86471{{"`Numpy Left Shift Function`"}} python/lists -.-> lab-86471{{"`Numpy Left Shift Function`"}} python/tuples -.-> lab-86471{{"`Numpy Left Shift Function`"}} python/sets -.-> lab-86471{{"`Numpy Left Shift Function`"}} python/importing_modules -.-> lab-86471{{"`Numpy Left Shift Function`"}} python/numerical_computing -.-> lab-86471{{"`Numpy Left Shift Function`"}} python/build_in_functions -.-> lab-86471{{"`Numpy Left Shift Function`"}} numpy/1d_array -.-> lab-86471{{"`Numpy Left Shift Function`"}} numpy/data_array -.-> lab-86471{{"`Numpy Left Shift Function`"}} numpy/bool_idx -.-> lab-86471{{"`Numpy Left Shift Function`"}} numpy/fancy_idx -.-> lab-86471{{"`Numpy Left Shift Function`"}} end

Import NumPy Library

First, we must import the NumPy library to use the left_shift() function.

import numpy as np

Use left_shift() on a Single Value

The left_shift() function is used to shift the bits to the left of a single integer value by the specified number of bits. Here’s an example:

input_num = 40
bit_shift = 2

output = np.left_shift(input_num, bit_shift)

print(f"After shifting {bit_shift} bits to the left, the value is: {output}")

Output:

After shifting 2 bits to the left, the value is: 160

Use left_shift() on an Array of Values

The left_shift() function can also be used on an array of integer values. In this case, the function will perform the left shift operation on each element in the array. Here’s an example:

input_arr = np.array([2, 8, 10])
bit_shift = np.array([3, 4, 5])

output = np.left_shift(input_arr, bit_shift)

print(f"After shifting the bits to the left, the array is:\n{output}")

Output:

After shifting the bits to the left, the array is:
[ 16 128 320]

The left_shift() function applied the left shift operation on each element of both arrays.

Specify an Output Array

You can specify an output array to store the results of the left shift operation. If you provide an output array, the function will update that array instead of allocating a new one. The output array must be broadcastable to the same shape as the input array. Here’s an example:

input_arr = np.array([2, 8, 10])
bit_shift = np.array([3, 4, 5])

output = np.zeros_like(input_arr, dtype=np.int64)

np.left_shift(input_arr, bit_shift, out=output)

print(f"After shifting the bits to the left, the output array is:\n{output}")

Output:

After shifting the bits to the left, the output array is:
[ 16 128 320]

Specify a Condition on the Output

You can also specify a condition to the where parameter to set the values of the output array. The where parameter is a boolean array which is broadcastable to the input arrays. Here’s an example:

input_arr = np.array([2, 8, 10])
bit_shift = np.array([3, 4, 5])

output = np.zeros_like(input_arr, dtype=np.int64)
condition = np.array([True, False, True])

np.left_shift(input_arr, bit_shift, out=output, where=condition)

print(f"After shifting the bits to the left, the output array is:\n{output}")

Output:

After shifting the bits to the left, the output array is:
[ 16   8 320]

The where parameter set the first and third elements of the output array according to the condition we specified.

Summary

This tutorial provided an overview of the left_shift() function of the NumPy library. We explained the basic syntax and parameters and then demonstrated the values returned by this function. We also provided code examples for using the function on a single value and an array of values and how to specify an output array and a condition on the output.

Other Python Tutorials you may like