Closed
Description
From pytorch/pytorch#59544 (comment):
For a left shift it doesn't matter whether these are logical or arithmetic shifts (https://en.wikipedia.org/wiki/Arithmetic_shift#Equivalence_of_arithmetic_and_logical_left_shifts_and_multiplication), but for a right shift it does matter.
Is this a logical or an arithmetic shift?
The <<
, >>
operators, numpy and pytorch are all consistent with each it looks like:
>>> 10 << 1
20
>>> 10 >> 1
5
>>> -10 >> 1
-5
>>> -11 >> 1
-6
>>> t = torch.Tensor([10])
>>> t
tensor([10.])
>>> t << 1
tensor([20.])
>>> t >> 1
tensor([5.])
>>> t = torch.Tensor([-10])
>>> t << 1
tensor([-20.])
>>> t >> 1
tensor([-5.])
>>> def f(x, shift=1):
... x2 = np.right_shift(x, shift)
... print(f'Original ({x}): {np.binary_repr(x)}')
... print(f'Shifted ({x2}): {np.binary_repr(x2)}')
...
...
>>> f(10)
Original (10): 1010
Shifted (5): 101
>>> f(-10)
Original (-10): -1010
Shifted (-5): -101
IIRC this is arithmetic shifting for negative integers.
@kgryte could you verify and add a description to the bitwise_right_shift
documentation?
Metadata
Metadata
Assignees
Labels
No labels