How to use the BitShift operator in Pytorch?

Viewed 878

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

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.

Related