torch.bitwise_xor
function bitwise_xor<S1 extends Shape, S2 extends Shape>(input: Tensor<S1>, other: Tensor<S2>): Tensor<BroadcastShape<S1, S2>>Element-wise bitwise XOR (exclusive OR): sets bit to 1 where operands differ.
Result has 1 only where operands have different bits. Useful for:
- Difference detection: Find which bits changed between values
- Parity checking: Detect single-bit errors (Hamming codes)
- Toggling bits: Flip specific bits in a value
- Comparing values: Which bits differ between two values
- Cryptography: Used in cipher operations and key scheduling
- Error detection: Single-bit error detection in communication
- Checksum computation: XOR-based checksums and parity bits
Binary behavior: For each bit position, result is 1 if operands differ, 0 if same.
Integer-only: Works with int32, int8, uint8, uint32, etc. Not valid for floats.
- Integer tensors only: Must use int8, uint8, int32, uint32 dtypes
- Self-inverse: a XOR a = 0; XOR is self-inverse operation
- Commutative: a XOR b = b XOR a (order doesn't matter)
- Associative: (a XOR b) XOR c = a XOR (b XOR c)
- Symmetric difference: In set theory, XOR is symmetric difference
- Parity: Number of 1s in XOR result indicates parity difference
- Integer-only: Cannot use with float32 or float64
- XOR twice cancels: Good for encryption but not secure
- Not commutative in practice: While logically commutative, different order can affect performance
Parameters
inputTensor<S1>- First integer tensor (any shape)
otherTensor<S2>- Second integer tensor (must be broadcastable with input)
Returns
Tensor<BroadcastShape<S1, S2>>– Integer tensor with shape = broadcast(input, other)Examples
// Basic bitwise XOR
const x = torch.tensor([5, 6, 7], { dtype: 'int32' }); // Binary: 101, 110, 111
const y = torch.tensor([3, 3, 3], { dtype: 'int32' }); // Binary: 011, 011, 011
torch.bitwise_xor(x, y); // [6, 5, 4] = Binary: 110, 101, 100// Detecting bit changes: find which bits changed
const version1 = torch.tensor([0b11010110], { dtype: 'uint8' });
const version2 = torch.tensor([0b11000110], { dtype: 'uint8' }); // Bit 4 changed
const changes = torch.bitwise_xor(version1, version2);
// [0b00010000] - only bit 4 differs// Toggling bits: flip specific bits (use XOR to invert)
const flags = torch.tensor([0b10101010], { dtype: 'uint8' });
const toggle_mask = torch.tensor([0b11110000], { dtype: 'uint8' }); // Flip top 4 bits
const toggled = torch.bitwise_xor(flags, toggle_mask);
// [0b01011010] - top 4 bits inverted, bottom 4 unchanged// Parity checking: detect single-bit errors
const original = torch.tensor([0b10110101], { dtype: 'uint8' });
const received = torch.tensor([0b10110001], { dtype: 'uint8' }); // Bit 2 flipped
const error_bits = torch.bitwise_xor(original, received);
// [0b00000100] - error in bit 2// Simple encryption: XOR cipher (educational only, not secure!)
const plaintext = torch.tensor([0x41, 0x42, 0x43], { dtype: 'uint8' }); // "ABC"
const key = torch.tensor([0x55, 0x55, 0x55], { dtype: 'uint8' });
const ciphertext = torch.bitwise_xor(plaintext, key);
// Decryption: XOR again with same key to recover plaintext
const decrypted = torch.bitwise_xor(ciphertext, key);
// Back to original plaintextSee Also
- PyTorch torch.bitwise_xor(input, other)
- bitwise_and - AND: set bit to 1 only where both have 1
- bitwise_or - OR: set bit to 1 if either has 1
- bitwise_not - NOT: invert all bits
- bitwise_left_shift - Left shift: multiply by powers of 2
- bitwise_right_shift - Right shift: divide by powers of 2