NumPy - left_shift() function
The Bitwise left shift operator (<<) takes the two numbers and left shift the bits of first operand by number of place specified by second operand. For example: for left shifting the bits of x by y places, the expression (x<<y) can be used. It is equivalent to multiplying x by 2y.
The example below describes how left shift operator works:
1000 << 2 returns 4000 (In Binary) 1000 -> 1111101000 << 2 | left shift the bits ----- V by 2 places 4000 <- 111110100000 (In Binary)
The NumPy left_shift() function shifts the bits of an integer to the left. Bits are shifted to the left by appending y 0s at the right of x. This operation is equivalent to multiplying x by 2y. This ufunc implements the C/Python operator <<.
Syntax
numpy.left_shift(x, y, out=None)
Parameters
x |
Required. Specify the input array. |
y |
Required. Specify the number of zeros to append to x. Has to be non-negative. If x.shape != y.shape, they must be broadcastable to a common shape. |
out |
Optional. Specify a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. |
Return Value
Returns x with bits shifted y times to the left. This is a scalar if both x and y are scalars.
Example:
In the example below, the left_shift() function is used to compute the left shift operation using two scalars.
import numpy as np x = 1000 y = 2 #left shift operation z = np.left_shift(x, y) #Displaying the result print("z =", z)
The output of the above code will be:
z = 4000
Example:
The left_shift() function can be used with arrays where it computes the left shift operation of the array element-wise.
import numpy as np x = np.array([[10, 20],[30, 40]]) y = np.array([[0, 1],[2, 3]]) #left shift operation z = np.left_shift(x, y) #Displaying the result print("z =") print(z)
The output of the above code will be:
z = [[ 10 40] [120 320]]
❮ NumPy - Binary Operators