Numpy right_shift() Function



The NumPy right_shift() function which is used to perform a bitwise right shift on the elements of an array. This function shifts the bits of each element in the input array to the right by a specified number of positions. When each bit shifted, a zero is inserted on the left.

This function can take an array or scalar as input and the number of positions to shift. The result is an array with the same shape as the input which contain the shifted values. It is often used for tasks involving bitwise manipulation such as in binary arithmetic or digital signal processing.

Syntax

Following is the syntax of Numpy right_shift() function −

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

Parameters

The Numpy right_shift() function takes the following parameters −

  • x1: Input values on which the right shift is applied.
  • x2: The number of positions the right shift to be applied.
  • 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 where the right shift has to be applied.
  • **kwargs: The parameters such as casting, order, dtype, subok are the additional keyword arguments, which can be used as per the requirement.

Return value

This function returns an array where each element is the result of shifting the bits of the corresponding element in input values to the right by the number of positions specified by shift.

Example 1

Following is the basic example of Numpy right_shift() function in which shifts the bits of an integer to the right by a specified number of positions, effectively dividing the number by powers of 2 −

import numpy as np

# Define an integer
number = 16  # Binary: 00010000

# Define the number of positions to shift
shift_positions = 2

# Perform the right shift operation
result = np.right_shift(number, shift_positions)

print('Original number:', number)
print('Binary representation:', np.binary_repr(number, width=8))
print(f'Right shift by {shift_positions} positions results in:', result)
print('Binary representation after shift:', np.binary_repr(result, width=8))  

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

Original number: 16
Binary representation: 00010000
Right shift by 2 positions results in: 4
Binary representation after shift: 00000100

Example 2

When dealing with bitwise shift operations, negative shift values are typically not supported. Attempting to use a negative shift value will usually result in an error or undefined behavior by depending on the implementation and the specific operation. Following is the example of it −

import numpy as np

# Define an array of integers
array = np.array([16, 32, 64, 128])

# Define a negative number of positions to shift
negative_shift_positions = -2

try:
    # Attempt to perform the right shift operation with a negative shift value
    result = np.right_shift(array, negative_shift_positions)
    print('Result with negative shift value:', result)
except ValueError as e:
    print('Error:', e)

Following is the output for negative right shift −

Result with negative shift value: [0 0 0 0]

Example 3

Here is the another example of performing the right shift operation of the bits using the right_shift() function −

import numpy as np

# Create a One-Dimensional array
arr = np.array([56, 87, 23, 92, 81, 98, 45, 98])

# Displaying our array
print("Array:", arr)

# Get the datatype
print("Array datatype:", arr.dtype)

# Get the dimensions of the Array
print("Array Dimensions:", arr.ndim)

# Get the shape of the Array
print("Our Array Shape:", arr.shape)

# Get the number of elements of the Array
print("Elements in the Array:", arr.size)

# The count of right shift
valRight = 3
# To shift the bits of an integer to the right, use the numpy.right_shift() method in Python Numpy
result = np.right_shift(arr, valRight)

# Display the result of the right shift operation
print("Result (right shift):", result)

Following is the output for right shift −

Array: [56 87 23 92 81 98 45 98]
Array datatype: int64
Array Dimensions: 1
Our Array Shape: (8,)
Elements in the Array: 8
Result (right shift): [ 7 10  2 11 10 12  5 12]
numpy_binary_operators.htm
Advertisements