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.

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-1: numpy.right_shift() function

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

Example-2: numpy.right_shift() function

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

Python Code Editor:

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