
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
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]