Understanding numpy.left_shift() function (4 examples)

Updated: March 1, 2024 By: Guest Contributor Post a comment

Overview

The numpy.left_shift() function, part of the NumPy library, allows users to efficiently shift the bits of an array of integers to the left, a fundamental process in bit manipulation and computer programming. This article will guide you through understanding and effectively using the numpy.left_shift() function, illustrated with four progressively advanced examples.

Syntax:

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

Where:

  • x1: Input integers to shift left.
  • x2: Number of bits to shift by. Can be an array or a single integer.
  • out: Optional. Where to store the result.
  • where: Optional. Condition for where to apply the operation.
  • casting, order, dtype, subok, signature, extobj: Optional. Advanced options for controlling operation behavior.

Example 1: Basic Usage

Initialize a simple array of integers and perform a basic left shift.

import numpy as np

arr = np.array([1, 2, 3, 4])
shifted_arr = np.left_shift(arr, 1)
print("Original array:", arr)
print("Shifted array:", shifted_arr)

Output:

Original array: [1 2 3 4]
Shifted array: [2 4 6 8]

Example 2: Working with Negative Numbers

The numpy.left_shift() function can also work with arrays containing negative numbers, though it’s important to understand how binary representation of negative numbers affects the outcome.

import numpy as np

arr_neg = np.array([-1, -2, -3, -4])
shifted_arr_neg = np.left_shift(arr_neg, 1)
print("Original array", arr_neg)
print("Shifted array", shifted_arr_neg)

Output:

Original array [-1 -2 -3 -4]
Shifted array [-2 -4 -6 -8]

Example 3: Array and Shifts of Different Lengths

It’s possible to apply different left shift values to each element in an array, using an array of shifts. Here’s how:

import numpy as np

arr = np.array([1, 2, 3, 4])
shifts = np.array([1, 2, 3, 4])
shifted_arr = np.left_shift(arr, shifts)
print("Output:", shifted_arr)

Output:

Output: [ 2 8 24 64]

This demonstrates the power of vectorization in NumPy, allowing for bulk operations on data arrays efficiently.

Example 4: Advanced Use – Combined with Other Operations

Combining left shift operations with other bitwise or mathematical operations opens up vast possibilities. Here’s an example that showcases such a combination:

import numpy as np

arr = np.array([1, 2, 3, 4])
shifted_and_added = np.left_shift(arr, 2) + 1
print("Shifted and added array:", shifted_and_added)

Output:

Shifted and added array: [ 5  9 13 17]

This process not only shifts the values but also transforms them through addition, showcasing the versatility of array operations with NumPy.

Conclusion

The numpy.left_shift() function is a powerful tool for manipulating numbers at the bitwise level. Through the examples shown, it’s clear that this function can be used in a straightforward manner or combined with other operations for advanced data processing. Understanding and utilizing the left shift can unlock new possibilities in data manipulation and analysis.