torch.bitwise_left_shift
function bitwise_left_shift<S1 extends Shape, S2 extends Shape>(input: Tensor<S1>, other: Tensor<S2>): Tensor<BroadcastShape<S1, S2>>Element-wise left bit shift: shifts bits left, filling with zeros.
Moves all bits left by N positions, filling right with zeros. Equivalent to multiplying by 2^N. Essential for:
- Fast multiplication: Shift left by N = multiply by 2^N (faster than multiply)
- Bit packing: Position values into specific bit ranges
- Encoding: Pack multiple small values into single integer
- Hardware registers: Set bits at specific positions
- Color encoding: Pack RGB values into 24-bit integer
- Scaling: Quick scaling of numbers by powers of 2
- Address calculations: Computing offsets in arrays/memory
Binary behavior: Each bit moves N positions left; zeros fill on the right.
Integer-only: Works with int32, int8, uint8, uint32, etc. Not valid for floats.
Overflow: Left-shifted bits that overflow are discarded (wrapping behavior).
- Equivalent to multiply: Left shift N = multiply by 2^N
- No sign extension: Unsigned shifts fill with 0
- Signed shifts: Behavior depends on implementation (may sign-extend)
- Overflow wraps: Extra bits discarded (wrapping behavior)
- Fast operation: Typically faster than multiply on hardware
- Broadcasting: Shift amounts broadcast like regular operators
- Integer-only: Cannot use with float32 or float64
- Overflow loss: Bits that shift out are lost (no saturation)
- Large shift amounts: Shifting by = bit width leads to undefined behavior
- Negative shifts: Behavior undefined; avoid negative shift amounts
- Sign bit issues: With signed integers, be careful of sign bit handling
Parameters
inputTensor<S1>- Tensor with values to shift (any shape)
otherTensor<S2>- Tensor with shift amounts in bits (broadcastable with input)
Returns
Tensor<BroadcastShape<S1, S2>>– Integer tensor with same shape as broadcast(input, other)Examples
// Basic left shift: multiplication by powers of 2
const x = torch.tensor([5, 2, 3], { dtype: 'int32' });
const shift = torch.tensor([1, 2, 1], { dtype: 'int32' });
torch.bitwise_left_shift(x, shift);
// [10, 8, 6] = [5*2, 2*4, 3*2]// Fast multiplication instead of * operator
const values = torch.tensor([100, 50, 25], { dtype: 'int32' });
const doubled = torch.bitwise_left_shift(values, torch.tensor([1, 1, 1]));
// [200, 100, 50] - same as multiplying by 2
// Bitwise shift is often faster than multiply on hardware// RGB color encoding: pack 3 bytes into 24-bit integer
const red = torch.tensor([255], { dtype: 'uint8' });
const green = torch.tensor([128], { dtype: 'uint8' });
const blue = torch.tensor([64], { dtype: 'uint8' });
const red_shifted = torch.bitwise_left_shift(red, 16); // Red in top 8 bits
const green_shifted = torch.bitwise_left_shift(green, 8); // Green in middle 8 bits
const color = torch.bitwise_or(torch.bitwise_or(red_shifted, green_shifted), blue);
// [0xFF8040] - packed RGB value// Bit packing: store multiple values in single integer
const values = torch.tensor([1, 2, 3, 4], { dtype: 'uint8' }); // 4 small values
const shifted = torch.tensor([0, 4, 8, 12], { dtype: 'uint8' }); // Positions
const packed = torch.bitwise_left_shift(values, shifted);
// [0x0001, 0x0020, 0x0300, 0x4000] - each in 4-bit field// Fast scaling: multiply by power of 2
const coefficients = torch.tensor([3, 7, 15, 31], { dtype: 'int32' });
const scale_power = 10; // Scale by 2^10 = 1024
const scaled = torch.bitwise_left_shift(coefficients, scale_power);
// [3*1024, 7*1024, 15*1024, 31*1024] - scaled valuesSee Also
- PyTorch torch.bitwise_left_shift(input, other)
- bitwise_right_shift - Right shift: divide by powers of 2
- bitwise_and - AND: result 1 where both bits are 1
- bitwise_or - OR: result 1 where either bit is 1