I have
a = np.array([300, 320, 616], dtype=np.uint16)
b = np.right_shift(a, 8, dtype=np.uint8)
which results in all-zero b
. Can you please explain this behavior?
I have
a = np.array([300, 320, 616], dtype=np.uint16)
b = np.right_shift(a, 8, dtype=np.uint8)
which results in all-zero b
. Can you please explain this behavior?
The reason you are seeing this behaviour is because you are using uint8. change it to uint16 to get the desired results.
https://docs.scipy.org/doc/numpy-1.13.0/user/basics.types.html
Basically, in uint8, the number is taken as a modulus of the max number 256. so 300 becomes 300%256=44
In: np.right_shift(a, 0, dtype=np.uint8)
Out: array([ 44, 64, 104], dtype=uint8)
for i in range(9):
print(np.right_shift(a, i, dtype=np.uint8))
will print the below
[ 44 64 104]
[22 32 52]
[11 16 26]
[ 5 8 13]
[2 4 6]
[1 2 3]
[0 1 1]
[0 0 0]
[0 0 0]