Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the BitShift operator in Pytorch?

Tags:

bit

pytorch

shift

Does anyone has an example of how to use the BitShift operator in Pytorch?

like image 617
Fabrice Auzanneau Avatar asked Oct 27 '25 20:10

Fabrice Auzanneau


1 Answers

Bitwise shift operator performs element-wise operation.

It works the same way it works in python, and numpy i.e. shift the bits of an integer to the left or right. The << and >> denotes the left and right shift respectively.

x = torch.tensor([16, 4, 1])
y = torch.tensor([1, 2, 3])
z = x << y
print(z)
tensor([32, 16,  8])

It's equivalent to 16 << 1 (np.left_shift(16, 1)), 4 << 2, and 1 << 3.

For each input element, if the attribute "direction" is "RIGHT", this operator moves its binary representation toward the right side so that the input value is effectively decreased. If the attribute "direction" is "LEFT", bits of binary representation moves toward the left side, which results the increase of its actual value.

This operator supports multidirectional (i.e., Numpy-style) broadcasting.

like image 114
kHarshit Avatar answered Oct 29 '25 16:10

kHarshit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!