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.

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