torch.Tensor.Tensor.bitwise_left_shift
Tensor.bitwise_left_shift(other: number): Tensor<S, D, Dev>Tensor.bitwise_left_shift(other: Tensor | number): Tensor<DynamicShape, D, Dev>Bitwise left shift operation (element-wise binary left shift).
Performs bitwise left shift: shifts bits left and fills right with zeros. Equivalent to multiplying by 2^shift_amount. Only works with integer dtypes.
Definition: x << n moves each bit n positions to the left, filling right with zeros. Equivalent to x * 2^n for positive x.
Use Cases:
- Efficient multiplication by powers of 2
- Bit manipulation and packing
- Low-level integer algorithms
- Signal processing and data encoding
- Scaling integer values
- Integer only: Works only with int8, uint8, int32, uint32 dtypes.
- Efficient: Much faster than regular multiplication.
- Overflow: Large shifts may cause overflow (bits lost from left).
- Broadcasting: Other broadcasts with self.
- Large shift amounts may cause overflow and data loss.
- Shift amount should be less than the bit width of the type.
Parameters
othernumber- Integer tensor containing shift amounts (broadcastable)
Returns
Tensor<S, D, Dev>– Integer tensor with left-shifted values, same shapeExamples
// Basic left shift (multiply by powers of 2)
const x = torch.tensor([1, 2, 3, 4], { dtype: 'int32' });
x.bitwise_left_shift(torch.tensor([1, 2, 3, 4], { dtype: 'int32' }));
// [2, 8, 24, 64] = [1*2, 2*4, 3*8, 4*16]
// Efficient multiplication
const value = torch.tensor([5], { dtype: 'int32' });
value.bitwise_left_shift(torch.tensor([3])); // 5 * 8 = 40
// Bit packing
const red = torch.tensor([255], { dtype: 'uint8' });
const green = torch.tensor([128], { dtype: 'uint8' });
const shifted = green.bitwise_left_shift(torch.tensor([8])); // 0x8000See Also
- PyTorch torch.bitwise_left_shift()
- bitwise_right_shift - Shift right (division by powers of 2)