Numpy bitwise_right_shift() Function



The numpy bitwise_right_shift() function which is used to perform a bitwise right shift operation on each element of the input array.

This function shifts the bits of each integer to the right by a specified number of positions, effectively dividing the number by powers of 2.

In this function positive integers, it results in integer division and for signed integers it maintains the sign by performing an arithmetic shift.

This function handles arrays of various integer types and returns an array with the same shape and type. It is equivalent to using the right shift operator >> in Python.

Syntax

Following is the syntax of Numpy bitwise_right_shift() function −

numpy.bitwise_right_shift(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature])

Parameters

The Numpy bitwise_right_shift() function takes the following parameters −

  • x1: Input values on which the right shift is applied.
  • x2: The number of positions to shift right of the input values x1.
  • out(ndarray, None or tuple of ndarray and None, optional): The location where the result is stored.
  • where(array_like, optional): This is the condition which broadcast over the input.
  • **kwargs: The parameters such as casting, order, dtype and subok are the additional keyword arguments, which can be used as per the requirement.

Return value

This function returns an array where each element has been shifted right.

Example 1

Following is the basic example of Numpy bitwise_right_shift() function in which Perform a bitwise right shift on the integer 18 by 2 positions −

import numpy as np

# Define the integer and the shift amount
x = 18
shift = 2

# Perform the bitwise right shift operation
result = np.right_shift(x, shift)
print(result)    

Below is the output of bitwise_right_shift() function applied on the integer 18 −

4

Example 2

When performing a bitwise right shift with an array of shift values, NumPy allows us to shift each element of the input array by a different number of positions specified by another array. Following is the example of it −

import numpy as np

# Define the input array and the array of shift values
x = np.array([16, 32, 64])
shift_values = np.array([1, 2, 3])

# Perform the bitwise right shift operation
result = np.right_shift(x, shift_values)
print(result)  

Following is the output for bitwise right shift −

[8 8 8]
numpy_binary_operators.htm
Advertisements