torch.Tensor.Tensor.bitwise_xor
Tensor.bitwise_xor(other: number): Tensor<S, D, Dev>Tensor.bitwise_xor(other: Tensor | number): Tensor<DynamicShape, D, Dev>Bitwise XOR operation (element-wise binary XOR).
Performs bitwise XOR between corresponding elements of two integer tensors. Each bit position is XOR-ed independently: result bit is 1 if input bits differ. Only works with integer dtypes (int8, int32, uint8, uint32).
Use Cases:
- Toggling/flipping bits
- Comparing bits (finding differences)
- Binary parity operations
- Cryptography and checksums
- Error detection
- Integer only: Works only with int8, uint8, int32, uint32 dtypes.
- Element-wise: Each element is XOR-ed independently.
- Broadcasting: Other broadcasts with self.
Parameters
othernumber- Integer tensor to XOR with (broadcastable)
Returns
Tensor<S, D, Dev>– Integer tensor with bitwise XOR result, same shapeExamples
// Basic bitwise XOR
const a = torch.tensor([0b1100, 0b1010], { dtype: 'int32' });
const b = torch.tensor([0b1010, 0b1010], { dtype: 'int32' });
a.bitwise_xor(b); // [0b0110, 0b0000] = [6, 0]
// Toggle specific bits
const value = torch.tensor([0b11110000], { dtype: 'uint8' });
const toggle = torch.tensor([0b10101010], { dtype: 'uint8' });
value.bitwise_xor(toggle); // [0b01011010] - toggled bitsSee Also
- PyTorch torch.bitwise_xor()
- bitwise_and - Bitwise AND
- bitwise_or - Bitwise OR
- bitwise_not - Bitwise NOT