PyTorch .bitwise_or()
Published May 12, 2025
Contribute to Docs
In PyTorch, the .bitwise_or() function computes the element-wise bitwise OR operation between two input tensors. For each pair of bits in the binary representation of the input elements, it returns 1 if at least one of the bits is 1, and 0 otherwise. This function supports integer tensors (signed or unsigned) and boolean tensors (where it performs a bitwise OR that behaves like a logical OR).
Syntax
torch.bitwise_or(input, other, *, out=None)
Parameters:
input: The first input tensor. Must be of integer or boolean type.other: The second input tensor. Must be broadcastable withinputand of the same type.out(Optional): A tensor for storing the output result.
Return value:
The .bitwise_or() function returns a new tensor containing the result of applying the bitwise OR operation to each pair of elements in the input tensors.
Example: Using torch.bitwise_or() with Integer and Boolean Tensors
The following example illustrates the usage of the .bitwise_or() function in PyTorch:
import torch# Integer tensor exampletensor1 = torch.tensor([5, 3], dtype=torch.int32) # Binary: 0101, 0011tensor2 = torch.tensor([3, 7], dtype=torch.int32) # Binary: 0011, 0111result_int = torch.bitwise_or(tensor1, tensor2)print(result_int)# Boolean tensor exampletensor_bool1 = torch.tensor([True, False])tensor_bool2 = torch.tensor([False, False])result_bool = torch.bitwise_or(tensor_bool1, tensor_bool2)print(result_bool)
The above code produces the following output:
tensor([7, 7], dtype=torch.int32)tensor([ True, False])
In this example:
5 | 3(binary0101|0011) results in0111(decimal7).3 | 7(binary0011|0111) results in0111(decimal7).True | Falseevaluates toTrue.False | Falseevaluates toFalse.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
- Intermediate.3 hours