.bitwise_left_shift()
Anonymous contributor
Published Jun 4, 2025
Contribute to Docs
In PyTorch, the .bitwise_left_shift()
function shifts each element of the input tensor to the left by a specified number of bits. Both operands must be of an integral type (e.g., int32
, int64
). Shifting by a negative number of bits returns 0
.
Syntax
torch.bitwise_left_shift(input, other, *, out=None)
Parameters:
input
: The first input tensor; must be of an integral type.other
: The second input (can be a tensor or integer).out
(Optional): A tensor where the output will be stored.
Return value:
Returns a tensor of the same shape as the broadcasted inputs, with each element being the result of left-shifting the corresponding element in input by the amount in other.
Example
This example shows how to use .bitwise_left_shift()
on a tensor:
import torch# Create two tensorsa = torch.tensor([-1, 4, 3], dtype=torch.int8)b = torch.tensor([1, 0, 3], dtype=torch.int8)# Apply bitwise_left_shift on two tensorsc = torch.bitwise_left_shift(a,b)print("c =",c)# Apply bitwise_left_shift on a tensor and numberd = torch.bitwise_left_shift(a, 2)print("d =",d)
The code produces this output:
c = tensor([-2, 4, 24], dtype=torch.int8)d = tensor([-4, 16, 12], dtype=torch.int8)
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours