Numpy left_shift() Function



The NumPy left_shift() function is used to perform bitwise left shift operations on the elements of an array.

When two arrays or an array and a scalar are given then it shifts the bits of each element in the first array to the left by the number of positions specified in the second array or scalar. The result is equivalent to multiplying the array elements by 2** shift.

  • This function supports broadcasting by allowing operations on arrays of different shapes. It's useful in low-level data manipulation such as binary encoding, image processing and operations requiring precise bit control.
  • In the context of bitwise operations the negative shift values are generally used with right shift operations where the bits are shifted to the right.
  • In the left shift operations the negative values do not have a meaningful interpretation and the behavior may be inconsistent or undefined.

Syntax

Following is the syntax of Numpy left_shift() function −

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

Parameters

Following are the Parameters of Numpy left_shift() function −

  • x1(array_like): The input array on which the left shift operation is performed.
  • x2(array_like or int): The number of bits to shift left each element of x1. It can be a single integer or an array of the same shape as x1.
  • out(Optional): This is an optional output array where the result is stored
  • where(Optional): A condition to determine where the shift operation should be performed. The result is computed where this condition is True.
  • **kwargs: The parameters casting, order, dtype, subok comes under the additional keyword arguments.

Return value

The function returns an array where each element is the result of left shifting the bits corresponding to the original array

Example 1

Following is the basic example of Numpy left_shift() function in which single integer of the array is shifted −

import numpy as np
# Shift the bits of the integer 5 to the left by 1 position
result = np.left_shift(5, 1)
print(result)  

Below is the output of left_shift() function −

10

Example 2

We can perform scalar shift uniformly across all elements of the array by shifting the bits of each element to the left by the specified number of positions. Here in this example we are shifting the array by 3 bits −

import numpy as np

# Define an array of integers
array = np.array([5, 10, 15, 20])

# Define the scalar shift value
shift = 3

# Perform the left shift operation
result = np.left_shift(array, shift)

print("Original array:", array)
print("Shifted array:", result)

Following is the output for the above example −

Original array: [ 5 10 15 20]
Shifted array: [ 40  80 120 160]

Example 3

Below is the example which performs a left shift on the number 10 by two positions and displays the results which includes the binary representations of the original and shifted values −

import numpy as np 

print('Left shift of 10 by two positions:') 
print(np.left_shift(10, 2))  

print('Binary representation of 10:') 
print(np.binary_repr(10, width=8)) 

print('Binary representation of 40:') 
print(np.binary_repr(40, width=8))  

Following is the output for the above example −

Left shift of 10 by two positions:
40
Binary representation of 10:
00001010
Binary representation of 40:
00101000

Example 2

Here in this example left_shift() does not handle negative shifts in a standard way and the result may not be as expected for left shifts. Instead the results are often not meaningful and may default to the same value as seen in this example −

import numpy as np

# Define an array of integers
array = np.array([16, 32, 64, 128])

# Define the array of shift values, including negative values
shifts = np.array([-1, -2, -3, -4])

# Perform the left shift operation
result = np.left_shift(array, shifts)

print('Original array:', array)
print('Shift values:', shifts)
print('Shifted array:', result) 

Here is the output of the above example −

Original array: [ 16  32  64 128]
Shift values: [-1 -2 -3 -4]
Shifted array: [0 0 0 0]
numpy_binary_operators.htm
Advertisements