Pytorch Torch Bitwise_Or
## PyTorch `torch.bitwise_or` Reference
The `torch.bitwise_or` function in PyTorch computes the element-wise bitwise OR of two tensors. It is a highly efficient operation commonly used for low-level binary data manipulation, masking, and boolean logic operations.
---
## Introduction
`torch.bitwise_or` performs a bitwise OR operation on each pair of corresponding elements in the input tensors.
* **For Integer Tensors:** It compares the binary representation of each element. The resulting bit is set to `1` if at least one of the corresponding bits of the operands is `1`. Otherwise, it is set to `0`.
* **For Boolean Tensors:** It computes the logical OR operation (equivalent to `+` or `logical_or` behavior).
---
## Syntax and Parameters
### Syntax
```python
torch.bitwise_or(input, other, *, out=None) -> Tensor
```
### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| **`input`** | `Tensor` or `Scalar` | The first input tensor or a scalar value. |
| **`other`** | `Tensor` or `Scalar` | The second input tensor or a scalar value. |
| **`out`** | `Tensor` (Optional) | The output tensor where the result will be written. |
### Supported Data Types
* **Integer types:** `torch.int8`, `torch.uint8`, `torch.int16`, `torch.int32`, `torch.int64`
* **Boolean type:** `torch.bool`
---
## Code Examples
### Example 1: Bitwise OR with Integer Tensors
This example demonstrates how the bitwise OR operation works on 8-bit integers.
```python
import torch
# Define two 8-bit integer tensors
# 5 in binary: 00000101
# 3 in binary: 00000011
# 7 in binary: 00000111
x = torch.tensor([5, 3, 7], dtype=torch.int8)
# 1 in binary: 00000001
# 3 in binary: 00000011
# 4 in binary: 00000100
y = torch.tensor([1, 3, 4], dtype=torch.int8)
# Perform element-wise bitwise OR:
# 5 | 1 -> 00000101 | 00000001 = 00000101 (5)
# 3 | 3 -> 00000011 | 00000011 = 00000011 (3)
# 7 | 4 -> 00000111 | 00000100 = 00000111 (7)
result = torch.bitwise_or(x, y)
print("Result Tensor:")
print(result) # Output: tensor([5, 3, 7], dtype=int8)
```
### Example 2: Logical OR with Boolean Tensors
When applied to boolean tensors, `torch.bitwise_or` behaves as a standard logical OR operation.
```python
import torch
# Define two boolean tensors
a = torch.tensor([True, False, True, False], dtype=torch.bool)
b = torch.tensor([True, True, False, False], dtype=torch.bool)
# Perform logical OR
result = torch.bitwise_or(a, b)
print("Boolean OR Result:")
print(result) # Output: tensor([ True, True, True, False])
```
### Example 3: Tensor and Scalar Bitwise OR
You can also perform a bitwise OR between a tensor and a scalar value. PyTorch will automatically broadcast the scalar to match the shape of the tensor.
```python
import torch
# Define a tensor
x = torch.tensor([1, 2, 4], dtype=torch.int32)
# Perform bitwise OR with a scalar value of 2
# 1 | 2 -> 01 | 10 = 11 (3)
# 2 | 2 -> 10 | 10 = 10 (2)
# 4 | 2 -> 100 | 010 = 110 (6)
result = torch.bitwise_or(x, 2)
print("Scalar Bitwise OR Result:")
print(result) # Output: tensor([3, 2, 6], dtype=int32)
```
---
## Important Considerations
1. **Broadcasting Support:** `torch.bitwise_or` supports PyTorch's broadcasting semantics. If the shapes of `input` and `other` do not match but are compatible, they will be broadcast to a common shape.
2. **In-place Operation:** If you want to perform the operation in-place and modify the original tensor, you can use `input.bitwise_or_(other)` or the operator shorthand `input |= other`.
3. **Floating-Point Types:** Bitwise operations are not defined for floating-point types (e.g., `torch.float32`, `torch.float64`). Attempting to use them will result in a `RuntimeError`.
YouTip