# Bitwise-XOR of all elements in a multi-dimensional Tensor

Hi everyone!
Does PyTorch provide a way to do the bitwise-xor of all elements in a tensor and return the output value?

I need to do this for a tensor of tensors, hence need a way to parallelize this computation, instead of using loops. Any kind of help would be great.

Do you mean

``````import torch

x = torch.rand(2, 3, 4, 5) > 0.5
y = torch.rand(3, 4, 5) > 0.5
print(x)
print(y)
z = x | y
print(z)
``````

Hi @Eta_C
No, actually I have to do something like this:

``````import torch

# Sample tensor
X = torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
lst = []
for i in X:
XorI = 0
for elem in i:
XorI = elem ^ XorI
lst.append(XorI)
``````

But without loops.

P.S. It would be better if thereâ€™s way for more than 2 dimensions as well, where I can reduce in 1 specified direction.

Sorry, since pytorch only have `logical_xor` and `bitwise_xor`, I have to find some â€śmath expressionâ€ť for this operator. See here, it is too complicated!

I think the best way is to make a pytorch extension.

1 Like

See here , it is too complicated!

Yeah, this one seems a bit too complicated.

I think the best way is to make a pytorch extension .