torch.bitwise_right_shift
function bitwise_right_shift<S1 extends Shape, S2 extends Shape>(input: Tensor<S1>, other: Tensor<S2>): Tensor<BroadcastShape<S1, S2>>Element-wise right bit shift: shifts bits right, filling with zeros (unsigned) or sign (signed).
Moves all bits right by N positions, filling left with zeros (unsigned) or sign bit (signed). Equivalent to dividing by 2^N and truncating. Essential for:
- Fast division: Shift right by N = divide by 2^N (faster than divide)
- Bit extraction: Extract specific bits from a value
- Unpacking: Unpack multiple small values from single integer
- Scaling down: Quick scaling by powers of 2
- Hardware registers: Extract bit fields from register
- Color decoding: Extract RGB components from packed integer
- Normalization: Divide by power-of-2 scale factors
Binary behavior: Each bit moves N positions right; zeros fill on left (unsigned) or sign extends (signed).
Integer-only: Works with int32, int8, uint8, uint32, etc. Not valid for floats.
Signed vs unsigned: Signed integers may sign-extend; unsigned always fill with 0.
- Equivalent to divide: Right shift N = divide by 2^N (floor division)
- Zeros fill: Unsigned always fills with 0
- Sign extension: Signed integers may preserve sign bit
- Truncation: Fractional part discarded (floor behavior)
- Fast operation: Typically faster than divide on hardware
- Broadcasting: Shift amounts broadcast like regular operators
- Integer-only: Cannot use with float32 or float64
- Signed behavior: Different behavior for signed vs unsigned integers
- Large shift amounts: Shifting by = bit width leads to undefined behavior
- Negative shifts: Behavior undefined; avoid negative shift amounts
- Information loss: Bits shifted out are lost; cannot recover original
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 right shift: division by powers of 2
const x = torch.tensor([10, 8, 6], { dtype: 'int32' });
const shift = torch.tensor([1, 2, 1], { dtype: 'int32' });
torch.bitwise_right_shift(x, shift);
// [5, 2, 3] = [10/2, 8/4, 6/2]// Fast division instead of / operator
const values = torch.tensor([1024, 512, 256], { dtype: 'int32' });
const halved = torch.bitwise_right_shift(values, torch.tensor([1, 1, 1]));
// [512, 256, 128] - same as dividing by 2
// Bitwise shift is often faster than divide on hardware// RGB color decoding: extract components from packed value
const color = torch.tensor([0xFF8040], { dtype: 'uint32' });
const red = torch.bitwise_right_shift(color, 16); // Extract top 8 bits
const green = torch.bitwise_right_shift(color, 8); // Extract middle 8 bits
const blue = color; // Bottom 8 bits (no shift needed)
// red=0xFF, green=0x80, blue=0x40// Bit extraction: pull out specific bits
const packed = torch.tensor([0b11010110], { dtype: 'uint8' });
const upper_4 = torch.bitwise_right_shift(packed, 4); // [0b0000_1101] = 13
const lower_4 = torch.bitwise_and(packed, 0x0F); // [0b0000_0110] = 6// Unpacking: extract multiple values from single integer
const packed = torch.tensor([0x4321], { dtype: 'uint16' }); // 4 nibbles packed
const nibble3 = torch.bitwise_right_shift(packed, 12); // [0x0004]
const nibble2 = torch.bitwise_right_shift(packed, 8); // [0x0043] (needs masking)
const nibble1 = torch.bitwise_right_shift(packed, 4); // [0x0432] (needs masking)
const nibble0 = torch.bitwise_and(packed, 0x0F); // [0x0001]// Scaling down by power of 2
const measurements = torch.tensor([4096, 8192, 16384], { dtype: 'int32' });
const scale_power = 10; // Divide by 2^10 = 1024
const scaled = torch.bitwise_right_shift(measurements, scale_power);
// [4, 8, 16] - scaled down valuesSee Also
- PyTorch torch.bitwise_right_shift(input, other)
- bitwise_left_shift - Left shift: multiply by powers of 2
- bitwise_and - AND: mask to extract bits
- bitwise_not - NOT: invert bits