w3resource

NumPy Binary operations: right_shift() function

numpy.right_shift() function

The right_shift() function is used to shift the bits of an integer to the right. Bits are shifted to the right x2. Because the internal representation of numbers is in binary format, this operation is equivalent to dividing x1 by 2**x2.

Some common applications of numpy.right_shift() include:
Division: Performing fast division of integers by powers of two, as right-shifting a binary number by 'n' positions is equivalent to dividing the number by 2^n (ignoring the remainder).
Data Manipulation: Changing the position of bits in data, such as extracting specific fields from data records, adjusting bitmasks, or unpacking data structures.
Cryptography: Applying bitwise right shift operations during encryption or decryption processes, especially in algorithms that involve bit manipulation or key generation.
Error Detection and Correction: Implementing error detection and correction algorithms that involve right shift operations on binary data.

Version: 1.15.0

Syntax:

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

Parameter:

Name Description Required /
Optional
x1 Input values. Required
x2 Number of bits to remove at the right of x1. Required
out A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. Optional
where Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone. Optional
**kwargs For other keyword-only arguments.

Return value:

out: [ndarray, int]
Return x1 with bits shifted x2 times to the right. This is a scalar if both x1 and x2 are scalars.

Example: Bitwise right shift operation with NumPy and binary representation

>>> import numpy as np
>>> np.binary_repr(12)
'1100'
>>> np.right_shift(12, 2)
3
>>> np.binary_repr(7)
'111'

In the above code, the binary representation of the number 12 is displayed using the numpy.binary_repr() function, which is '1100'. Then, the numpy.right_shift() function is used to perform a bitwise right shift operation on the number 12, shifting it two positions to the right. The result of the right shift operation is 3. The binary representation of another number (7) is displayed using numpy.binary_repr(), which is '111'.

Example: Bitwise right shift operation with NumPy and varying shift amounts

>>> import numpy as np
>>> np.right_shift(12, [2,3,5])
array([3, 1, 0], dtype=int32)

In the above code, the integer 12 is right-shifted by the values in the array [2, 3, 5], resulting in a new array with the shifted values ([3, 1, 0]). Each element in the output array is the result of the right shift operation applied to the number 12 with the corresponding shift amount from the input array.

Python Code Editor:

Previous: left_shift()
Next: Bit packing packbits()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/numpy/binary-operations/right-shift.php