NumPy right_shift() function
In this tutorial, we will cover the
right_shift() function that is a binary operation of the Numpy library.
In Numpy, the
right_shift() function is mainly used to perform the right shift operation.
right_shift()function is mainly used in order to shift the bits of an integer to the right.
This function primarily shifts the bits in the binary representation of the operand to the right just by the specified position and also an equal number of 0s are appended from the left.
The syntax required to use this function is as follows:
numpy.right_shift(x1, x2, /, out, *, where, casting='same_kind', order='K',dtype,subok=True[, signature, extobj]) = <ufunc 'right_shift'>
As the internal representation of numbers is mainly in the binary format, thus right shift operation is equivalent to dividing x1 by 2**x2.
Let us now take a look at the parameters of this function:
This parameter is used to represent the input value and it is in the form of an array.
This parameter is used to indicate the number of bits to remove at the right of x1. If
x1.shape != x2.shape, then they must be broadcastable to a common shape and that shape becomes the shape of the output.
This parameter mainly indicates a location in which the result is stored. If this parameter is provided, it must have a shape that the inputs broadcast to. If this parameter is either not provided or it is None then a freshly-allocated array is returned.
This parameter is used to indicate a condition that is broadcast over the input. At those locations where the condition is True, the out array will be set to the ufunc result, else the out array will retain its original value.
This function will return x1 with its bits shifted x2 times to the right. The returned value is a scalar if both x1 and x2 are scalars.
In the example below, we will illustrate the usage of
import numpy as np input_num = 40 bit_shift = 2 print ("The Input number is: ") print(input_num) print ("The Number of bit shift : ") print(bit_shift ) output = np.right_shift(input_num, bit_shift) print ("After the shifting of 2 bits to right : ") print(output)
The Input number is:
The Number of bit shift :
After the shifting of 2 bits to right :
Now we will show you a code snippet where we will apply
right_shift() on to an input array:
import numpy as np input_arr = [8, 28, 55] bit_shift =[3, 4, 2] print ("The Input array is: ") print(input_arr) print ("The Number of bit shift : ") print(bit_shift ) output = np.right_shift(input_arr, bit_shift) print ("After right shifting of bits,the output array is: ") print(output)
The Input array is:
[8, 28, 55]
The Number of bit shift :
[3, 4, 2]
After right shifting of bits,the output array is:
[ 1 1 13]
In this tutorial, we covered the
right_shift() function of the NumPy library. We covered its basic syntax and parameters and then the values returned by this function along with a few examples.