What is right_shift() in Numpy?

Overview

The right_shift() is a NumPy function used to shift the bits of an integer to the right. It can take either a number or Numpy array as an argument to right shift the bits.

Syntax

numpy.right_shift(x1,x2,out=None,where=True,**kwargs)

Parameters

It takes the following argument values.

  • x1: It is an array or integer like object.
  • x2: It is an array or integer like object that shows the number of bits that will shift right.
  • out=None: It is the object where computed results are stored.
  • where=True: If set to True, it means a universal function ufunc will be computed for a specific index, otherwise leave the values as it is in out.
  • **kwargs: It is a variable length keyword argument.

Return value

This function returns x1 with bits shifted x2 time to the right. It can either be an ndarray or integer value.

Code example 1

In the below code snippet, right_shift() takes the first argument x1=10 and second x2=1. It shifts 1010(binary: 0000 1010) to the right once, resulting in 55(binary: 0000 0101).

# Program to show working of
# right_shift() when arguments are int values
import numpy as np
# declaring two integers
x1= 10
x2= 1
# show numbers in binary representation
print("Binary of x1:", np.binary_repr(100))
print("Binary of x2:", np.binary_repr(1))
# invoking right_shift() and printing results
print("Right shift Result: ", np.right_shift(x1, x2))

Explanation

  • Lines 5 and 6: We define two variables x1 and x2 with values 1010 and 11 assigned to them, respectively.
  • Lines 8 and 9: We use np.binary_repr() to show a binary representation of argument value. Hence, it prints binary of 1010 and 11.
  • Line 11: We use np.right_shift() to shift x1 argument x2 times and show results to console.

Code example 2

Let's look at the code below:

# Program to show working of
# right_shift() when arguments are int values
import numpy as np
# defining integer and an array
x1= 5000
x2 = np.array([8,7,5])
# show numbers in binary representation
print("Binary Representation of x1:", np.binary_repr(10))
print("Binary Representation of x2:[", end=' ')
for i in range(0,len(x2)):
print(np.binary_repr(x2[i-1]), end=' ')
print("]")
# invoking right_shift() and printing results
print("Right shifted:", np.right_shift(x1, x2))

Explanation

  • Lines 5 and 6: We create x1 and x2 two variables with 50005000 and NumPy array [8,7,5][8,7,5], respectively.
  • Lines 8 to 12: We print x1, x2 values in binary representation.
  • Line 14: We invoke np.right_shift() to perform right shift operation on x1 to shift values x2 times.

Bitwise >>

In conventional procedural languages, the bit-wise operator >> is used to perform shift-right operations. Hence, we can also use the greater than> sign twice instead.

# include numpy as alias np
import numpy as np
# creating a constant variable
x1 = 10
# creating a numpy array
x2 = np.array([1,2,3])
# Using bit-wise operator
print(x1 >> x2)

Explanation

  • Line 4: We define x1 and assign 1010 value.
  • Line 6: We define x2, which contains a NumPy array [1,2,3][1,2,3].
  • Line 8: We use the bit-wise operator >> to perform the bitwise right shift.

Free Resources