What is numpy.left_shift() in Python?
Overview
The numpy.left_shift() method is used to shift bits to the left x2 times by appending 0s at the right of x1.
Note: The left shift operation is equivalent to the multiplying
x1by.
Syntax
numpy.left_shift(arr1,arr2,/,out=None,*,where=True,casting='same_kind',order='K',dtype=None,ufunc 'left_shift')
Parameters
It takes the following argument values.
arr1: Integer number or array_like instance.arr2: Integer number or array_like instance. It shows the number of 0s which will be appended tox1. Ifx1.shapeis not equal tox2.shape, then its dimensions should be able to be broadcasted.out: A location where results are stored. It can bendarray,None, and a tuple ofndarray. Its default value isNone.where: Array-like instance. It shows a condition where broadcasting is applied. Its default value isTrue.**kwargs: The other arguments are optional and show keyword argument values.
Return value
It returns either a scalar value or an integer array.
Case 1: Where x1 & x2 are both scalar values
# Case#1 where x1 & x2 both are scalar values.# loading numpy libraryimport numpy as np# defining x1 and x2 numbersx1= 10x2= 2# invoking left_shift() method to perform left shift operationout= np.left_shift(x1, x2)# print output on console.print("After left shifting 2 bit : ", out)
- Line 5–6: We define
x1andx2as integer values. - Line 8: We call
left_shift()withx1as10andx2as2to left shiftx1by 2 times. It prints40because the left shift is also equivalent to. - Line 10: We print the left-shifted number to the console.
Case 2: Where x1 is a scalar while x2 is an integer array
# Case#2 where x1 is scalar while x2 is integer array# loading numpy libraryimport numpy as np# defining an integer as well as integer arrayx1= 5x2= [1, 2, 3]# print numbers on consoleprint("Input number : ", x1)print("Number of bit shift : ", x2)# left x1, x2 timesout= np.left_shift(x1, x2)# print numbers on consoleprint("Output array after left shifting: ", out)
- Line 5–6: We define
x1as5(scalar) while we definex2as[1, 2, 3](integer array). - Line 8–9: We print
x1as well asx2to the console. - Line 11: The
np.left_shift(x1, x2)statement shiftsx1values,x2times at each index of arrays. - Line 13: We print the left-shifted number to the console.
Case 3: Where x1 & x2 are both integer arrays
# Case#3 where x1 & x2 both are integer arrays.# loading numpy libraryimport numpy as np# defining two integer arraysx1= [2, 8, 15]x2= [3, 4, 5]# shows above arrays on consoleprint("Input array : ", x1)print("Number of bit shift : ", x2)# invoking left_shift() to shift x1, x2 timesout= np.left_shift(x1, x2)# results on the consoleprint("Output array after left shifting: ", out)
- Line 5–6: We define
x1as well asx2as two integer arrays. - Line 8–9: We print the
x1andx2arrays to the console. - Line 11: The
np.left_shift(x1, x2)statement will left shiftx1array valuesx2times each time the number parallel to each index. - Line 13: We print the left-shifted results as integer array to the console:
[ 16 128 480].