NumPy - right_shift() function
The Bitwise right shift operator (>>) takes the two numbers and right shift the bits of first operand by number of place specified by second operand. For example: for right shifting the bits of x by y places, the expression (x>>y) can be used. It is equivalent to dividing x by 2y.
The example below describes how right shift operator works:
1000 >> 2 returns 250 (In Binary) 1000 -> 1111101000 >> 2 | right shift the bits ----- V by 2 places 250 <- 11111010 (In Binary)
The NumPy right_shift() function shifts the bits of an integer to the right. Bits are shifted to the right y. This operation is equivalent to dividing x by 2y. This ufunc implements the C/Python operator >>.
Syntax
numpy.right_shift(x, y, out=None)
Parameters
x |
Required. Specify the input array. |
y |
Required. Specify the number of bits to remove at the right of 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 right. This is a scalar if both x and y are scalars.
Example:
In the example below, the right_shift() function is used to compute the right shift operation using two scalars.
import numpy as np x = 1000 y = 2 #right shift operation z = np.right_shift(x, y) #Displaying the result print("z =", z)
The output of the above code will be:
z = 250
Example:
The right_shift() function can be used with arrays where it computes the right shift operation of the array element-wise.
import numpy as np x = np.array([[100, 200],[300, 400]]) y = np.array([[0, 1],[2, 3]]) #right shift operation z = np.right_shift(x, y) #Displaying the result print("z =") print(z)
The output of the above code will be:
z = [[100 100] [ 75 50]]
❮ NumPy - Binary Operators