torch.Tensor.Tensor.bitwise_right_shift
Tensor.bitwise_right_shift(other: number): Tensor<S, D, Dev>Tensor.bitwise_right_shift(other: Tensor | number): Tensor<DynamicShape, D, Dev>Bitwise right shift operation (element-wise binary right shift).
Performs bitwise right shift: shifts bits right and fills left with sign bit (arithmetic) or zeros (logical). Equivalent to dividing by 2^shift_amount (with truncation). Only works with integer dtypes.
Definition: x >> n moves each bit n positions to the right, filling left based on sign. Equivalent to x / 2^n (with truncation towards zero) for positive x.
Use Cases:
- Efficient division by powers of 2
- Bit extraction and field access
- Low-level integer algorithms
- Downsampling/decimation in signal processing
- Scaling integer values down
- Integer only: Works only with int8, uint8, int32, uint32 dtypes.
- Efficient: Much faster than regular division.
- Truncation: Divisions truncate towards negative infinity (floor division).
- Broadcasting: Other broadcasts with self.
- For signed types, sign bit extends (arithmetic shift, not logical).
- 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 right-shifted values, same shapeExamples
// Basic right shift (divide by powers of 2)
const x = torch.tensor([8, 16, 32, 64], { dtype: 'int32' });
x.bitwise_right_shift(torch.tensor([1, 2, 3, 4], { dtype: 'int32' }));
// [4, 4, 4, 4] = [8/2, 16/4, 32/8, 64/16]
// Efficient division
const value = torch.tensor([40], { dtype: 'int32' });
value.bitwise_right_shift(torch.tensor([3])); // 40 / 8 = 5
// Extract high byte
const color = torch.tensor([0xFF8000], { dtype: 'uint32' });
const red = color.bitwise_right_shift(torch.tensor([16])); // Extract red componentSee Also
- PyTorch torch.bitwise_right_shift()
- bitwise_left_shift - Shift left (multiply by powers of 2)