Signup/Sign In
LAST UPDATED: DECEMBER 17, 2020

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.

  • The 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.

Syntax of numpy.right_shift():

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.

Parameters:

Let us now take a look at the parameters of this function:

  • x1
    This parameter is used to represent the input value and it is in the form of an array.

  • x2
    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.

  • out
    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.

  • where
    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.

Returned Values:

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.

Example 1:

In the example below, we will illustrate the usage of right_shift() function:

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:
40
The Number of bit shift :
2
After the shifting of 2 bits to right :
10

Example 2:

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]

Summary

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.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.