Overview
- The numpy.right_shift() function works on array-likes and scalars. It shifts the bits of the elements of one array to the corresponding element number of times to the right.
- The effect of the shifting n bits to the right is equal to dividing the number by 2**n times.
Example
# Example Python program that uses numpy.right_shift()
# function to perform division on the elements
# of an ndarray by the the elements of another ndarray.
# result = left array element/2**right array element
import numpy
# The print() function will use this for printing
# the ndarrays in binary form
asBinary = lambda x: numpy.binary_repr(x, width=10)
binaryConvert = numpy.vectorize(asBinary)
# Creation of ndarray
leftArray = numpy.ndarray(shape = (1, 3), dtype = numpy.int32)
rightArray = numpy.ndarray(shape = (1, 3), dtype = numpy.int32)
# Init
leftArray[0][0] = 16
leftArray[0][1] = 255
leftArray[0][2] = 576
rightArray[0][0] = 4
rightArray[0][1] = 5
rightArray[0][2] = 6
# Apply right_shift() to perform division
rightShifted = numpy.right_shift(leftArray, rightArray)
print("Left array in decimal form:")
print(leftArray)
print("Right array in decimal form:")
print(rightArray)
print("Result of right_shift() in decimal form:")
print(rightShifted)
print("Left array in binary form:")
print(binaryConvert(leftArray))
print("Right array in binary form:")
print(binaryConvert(rightArray))
print("Result of right_shift() in binary form:")
print(binaryConvert(rightShifted))Output
Left array in decimal form:
[[ 16 255 576]]
Right array in decimal form:
[[4 5 6]]
Result of right_shift() in decimal form:
[[1 7 9]]
Left array in binary form:
[['0000010000' '0011111111' '1001000000']]
Right array in binary form:
[['0000000100' '0000000101' '0000000110']]
Result of right_shift() in binary form:
[['0000000001' '0000000111' '0000001001']]