torch.bitwise_or
function bitwise_or<S1 extends Shape, S2 extends Shape>(input: Tensor<S1>, other: Tensor<S2>): Tensor<BroadcastShape<S1, S2>>Element-wise bitwise OR: sets bit to 1 if either operand has 1.
Performs logical OR on individual bits of two integers. Result has 1 wherever either input has 1. Essential for:
- Flag combination: Merge multiple flags into single value
- Setting bits: Turn on specific bits in a value
- Union operations: Combine bit sets (OR = union in set theory)
- Configuration flags: Combine multiple configuration options
- Permissions: Merge user and group permissions
- Feature flags: Enable multiple features simultaneously
- Network routing: Combining subnet masks
Binary behavior: For each bit position, result is 1 if at least one input has 1.
Integer-only: Works with int32, int8, uint8, uint32, etc. Not valid for floats.
- Integer tensors only: Must use int8, uint8, int32, uint32 dtypes
- Inclusive operation: One OR either = either (includes both)
- Broadcasting: Shapes broadcast like regular operators
- Commutative: a OR b = b OR a (order doesn't matter)
- Associative: (a OR b) OR c = a OR (b OR c)
- Set union: In set theory, OR is equivalent to union operation
- Integer-only: Cannot use with float32 or float64
- Can set unexpected bits: Turning on bits without checking existing state
- Order-independent but not symmetric: Logically the same but different performance
Parameters
inputTensor<S1>- First integer tensor (any shape)
otherTensor<S2>- Second integer tensor (must be broadcastable with input)
Returns
Tensor<BroadcastShape<S1, S2>>– Integer tensor with shape = broadcast(input, other)Examples
// Basic bitwise OR
const x = torch.tensor([5, 6, 7], { dtype: 'int32' }); // Binary: 101, 110, 111
const y = torch.tensor([3, 3, 3], { dtype: 'int32' }); // Binary: 011, 011, 011
torch.bitwise_or(x, y); // [7, 7, 7] = Binary: 111, 111, 111// Flag combination: merging permissions
const user_flags = torch.tensor([0b00110000], { dtype: 'uint8' }); // User perms
const group_flags = torch.tensor([0b00001100], { dtype: 'uint8' }); // Group perms
const combined = torch.bitwise_or(user_flags, group_flags);
// [0b00111100] - both user and group permissions active// Setting specific bits: enable features
const features = torch.tensor([0b00000001], { dtype: 'uint8' }); // Feature 0 on
const new_features = torch.tensor([0b00000100], { dtype: 'uint8' }); // Feature 2 to enable
const enabled = torch.bitwise_or(features, new_features);
// [0b00000101] - features 0 and 2 now enabled// Union of bit sets: combining selections
const selected_odd = torch.tensor([0b01010101], { dtype: 'uint8' }); // Odd positions
const selected_prime = torch.tensor([0b10110010], { dtype: 'uint8' }); // Prime positions
const either = torch.bitwise_or(selected_odd, selected_prime);
// All positions that are odd OR prime// Network: combining IP subnet information
const base_config = torch.tensor([0x10, 0x20, 0x30, 0x40], { dtype: 'uint8' });
const optional_bits = torch.tensor([0x01, 0x02, 0x03, 0x04], { dtype: 'uint8' });
const full_config = torch.bitwise_or(base_config, optional_bits);
// Configuration with base and optional features enabledSee Also
- PyTorch torch.bitwise_or(input, other)
- bitwise_and - AND: set bit to 1 only where both have 1
- bitwise_xor - XOR: set bit to 1 where operands differ
- bitwise_not - NOT: invert all bits
- bitwise_left_shift - Left shift: multiply by powers of 2
- bitwise_right_shift - Right shift: divide by powers of 2