Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute
menu
close

Need Help? Talk to an Expert

+91 8000107255
Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute

Need Help? Talk to an Expert

+91 8000107255

torch.bitwise_or() Method

Home torch.bitwise_or() Method
PyTorch torch.bitwise_or() Method
  • Written by krunallathiya21
  • June 3, 2025
  • 0 Com
PyTorch

The torch.bitwise_or() method performs a bitwise OR operation on the binary representations of the integer values in the tensors. It supports both tensor-tensor operations and tensor-scalar operations.

Before we proceed, we need to understand how the bitwise OR works with 1 and 0.

Bitwise OR Truth Table

Input A Input B A | B (Output)
0 0 0
0 1 1
1 0 1
1 1 1
torch.bitwise_or()

As shown in the figure above, we defined two tensors with a single element. We have a tensor(10) and tensor(7) and we need to perform bitwise_or operation, it will be done like this:

The binary of the first tensor 10 is 1010.

The binary of the second tensor 6 is 0110.

Now, compare the first bits of both tensors. If input a is 1 and input b is 0, the output is 1. The output bits are based on the above table. Therefore, verify each output against the table mentioned above.

Check the second bits of both the tensors: 0 and 1, the output is 1.

For the third element of both tensors, 1 and 1, the output is 1.

For the fourth element of both tensors, 0 and 0, the output is 0.

That means the output is (1 1 1 0). It is the binary representation of 14.

So, the output tensor will be tensor(14).

Syntax

torch.bitwise_or(input, other, out=None)

Parameters

Argument Description
input (Tensor) It represents an input tensor. The first tensor.
other (Tensor or Scalar, optional) It represents the second input tensor or a scalar value to perform the bitwise OR with.
out (Tensor, optional)

It is an output tensor to store the result.

Element-wise bitwise OR between two tensors

torch.bitwise_or() with a 1D tensor

Let’s define two tensors with three elements each and perform a bitwise OR operation between the corresponding elements of the two tensors.

import torch

# Define two integer tensors
a = torch.tensor([10, 12, 14])  # Binary: [1010, 1100, 1110]
b = torch.tensor([6, 5, 3])     # Binary: [0110, 0101, 0011]

# Perform bitwise OR
result = torch.bitwise_or(a, b)
print(result)

# Output: tensor([14, 13, 15]) # Binary: [1110, 1101, 1111]

Based on the previously defined logic, 10 and 6 return 14, 12 and 5 return 13, and 14 and 3 return 15.

Bitwise OR with a Scalar

scalar value

If the first value is a tensor and the second value is a scalar, the .bitwise_or() method applies a bitwise OR operation between a tensor and a scalar value.

import torch

tensor = torch.tensor([1, 2, 3])  # Binary: 001, 010, 011

scalar = 4 # Binary: 100

result = torch.bitwise_or(tensor, scalar)  

print(result)  # Binary: 101, 110, 111
# Output: tensor([5, 6, 7])

Boolean tensors

Let’s define two boolean tensors containing True and False values and perform the bitwise_or operation.

import torch

boolean_a = torch.tensor([True, False, True, False])
boolean_b = torch.tensor([False, True, True, False])

# Perform element-wise logical OR operation
result_and = torch.bitwise_or(boolean_a, boolean_b)

print(result_and)
# Output: tensor([ True,  True,  True, False])
The result will be false if both elements are False. Otherwise, it will be True.

Unsupported Types

If your input tensor contains unsupported types (e.g., floating-point tensors), it will throw an error.

import torch

# floating-point tensors
a = torch.tensor([11.0, 21.0])
b = torch.tensor([70.0, 51.0])

try:
    output = torch.bitwise_or(a, b)
except RuntimeError as e:
    print(e)

# Output: "bitwise_or_cpu" not implemented for 'Float'

It only accepts integer-based tensor types. It does not work with floating points or complex numbers.

There is another function related to this one, called the torch.bitwise_and() method, which we covered on this website.

Post Views: 6
LEAVE A COMMENT Cancel reply
Please Enter Your Comments *

krunallathiya21

All Categories
  • JavaScript
  • Python
  • PyTorch
site logo

Address:  TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

sprintchasetechnologies@gmail.com

(+91) 8000107255.

ABOUT US
  • About
  • Team Members
  • Testimonials
  • Contact

Copyright by @SprintChase  All Rights Reserved

  • PRIVACY
  • TERMS & CONDITIONS