PyTorch .bitwise_xor()
Published May 12, 2025
Contribute to Docs
The .bitwise_xor() is used to calculate the bitwise XOR value of the inputs. The inputs should be either of boolean or of integer value. In the case of integral inputs, the values are converted to their binary value and then computed.
Syntax
torch.bitwise_xor(input, other, *, out=None)
Parameters:
input: The first input tensor with boolean or integer values to apply XOR on.other: The second input tensor or a single number.out: The output tensor to store the result.
Return value:
- A tensor containing the result of the element-wise bitwise XOR operation. The output tensor has the same shape as the broadcasted shape of
inputandother.
Example
The following example shows how to perform bitwise XOR on a tensor:
import torch# Create two tensorsa = torch.tensor([-1, 1, 0, -3], dtype=torch.int8)b = torch.tensor([7, 1, -5, 3], dtype=torch.int8)# Apply bitwise_xor on two tensorsc = torch.bitwise_xor(a,b)print("c =",c)# Apply bitwise_xor on a tensor and numberd = torch.bitwise_xor(b, -9)print("d =",d)
This produces the following output:
c = tensor([-8, 0, -5, -2], dtype=torch.int8)d = tensor([-16, -10, 12, -12], dtype=torch.int8)
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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours