XOR Calculator
Free Xorcalculator Calculator for number systems. Enter values to get step-by-step solutions with formulas and graphs.
Formula
A XOR B: output 1 when bits differ, 0 when same
XOR (exclusive or) compares each bit position of two numbers and outputs 1 if the bits are different, 0 if the same. Key properties: A XOR A = 0, A XOR 0 = A, XOR is commutative and associative.
Worked Examples
Example 1: Basic XOR Calculation
Problem: Calculate 42 XOR 27 and show the binary breakdown.
Solution: 42 in binary: 00101010\n27 in binary: 00011011\nXOR operation (1 where bits differ):\n 00101010\n 00011011\n --------\n 00110001 = 49\nHamming distance = 4 (four bits differ)
Result: 42 XOR 27 = 49 (0x31) | Hamming distance: 4
Example 2: XOR Swap Algorithm
Problem: Swap A=15 and B=9 using only XOR operations.
Solution: Start: A=15 (00001111), B=9 (00001001)\nStep 1: A = A XOR B = 15 XOR 9 = 6 (00000110)\nStep 2: B = A XOR B = 6 XOR 9 = 15 (00001111)\nStep 3: A = A XOR B = 6 XOR 15 = 9 (00001001)\nResult: A=9, B=15 (swapped without temp variable)
Result: After swap: A = 9, B = 15 | Verified using XOR self-inverse property
Frequently Asked Questions
What are the key properties of XOR that make it useful?
XOR has several remarkable mathematical properties. Self-inverse: A XOR A = 0 (any value XOR'd with itself is zero). Identity: A XOR 0 = A (XOR with zero preserves the value). These two properties together mean A XOR B XOR B = A, which is the basis for XOR encryption and swap algorithms. Commutativity: A XOR B = B XOR A. Associativity: (A XOR B) XOR C = A XOR (B XOR C). No information loss: given the result and one input, the other input can be recovered. XOR also has no carry propagation, making it the fastest arithmetic-like operation in hardware. These properties make XOR indispensable in cryptography, coding theory, and algorithm design.
How is XOR used in cryptography and encryption?
XOR is the foundation of many cryptographic systems because of its perfect balance and reversibility. The Vernam cipher (one-time pad) XORs plaintext with a random key of equal length, producing theoretically unbreakable encryption. Stream ciphers like RC4 and ChaCha20 generate a pseudorandom keystream and XOR it with plaintext. Block ciphers like AES use XOR extensively in their internal rounds and in modes of operation like CBC and CTR. The security comes not from XOR itself but from the key generation. XOR is ideal because it distributes uniformly: if either input is uniformly random, the output is uniformly random regardless of the other input. This property is unique among binary operations.
What is the Hamming distance and how does XOR help compute it?
The Hamming distance between two binary strings is the number of positions where the corresponding bits differ. XOR directly computes this: XOR the two values, then count the number of 1-bits (popcount) in the result. Since XOR produces 1 only where bits differ, the popcount of the XOR result gives the Hamming distance. For example, 42 (00101010) XOR 27 (00011011) = 49 (00110001), which has four 1-bits, so the Hamming distance is 4. Hamming distance is crucial in error-correcting codes (like Hamming codes), telecommunications for measuring signal degradation, and in machine learning for comparing binary feature vectors.
How does XOR differ from AND, OR, and other bitwise operations?
Each bitwise operation has distinct behavior. AND outputs 1 only when both inputs are 1, useful for masking specific bits. OR outputs 1 when either or both inputs are 1, useful for setting bits. XOR outputs 1 when inputs differ, useful for toggling bits and detecting changes. NAND and NOR are the complements of AND and OR respectively. XNOR (equivalence) is the complement of XOR, outputting 1 when inputs are the same. AND tends to clear bits, OR tends to set bits, and XOR tends to toggle bits. Only XOR is its own inverse (applying it twice returns to the original value). NAND and NOR are functionally complete, meaning any logic circuit can be built using only NAND or only NOR gates.
What is XOR used for in error detection and correction?
XOR is central to many error-detection schemes. Simple parity checking XORs all data bits together; if the result is 1, there is an odd number of 1-bits, and any single-bit error changes the parity. RAID 5 storage uses XOR to compute parity blocks: XOR all data drives together, and if one drive fails, XOR the remaining drives to recover the lost data. CRC (cyclic redundancy check) performs XOR-based polynomial division for robust error detection. Hamming codes place parity bits at power-of-2 positions and use XOR to both detect and correct single-bit errors. Internet checksums and TCP error detection also rely on XOR-related operations for data integrity verification.
How is XOR used in hash functions and checksums?
Hash functions frequently use XOR to combine multiple values into a single hash. Fowler-Noll-Vo (FNV) hashing XORs each input byte with the running hash. Many programming languages implement hash combination using XOR with bit rotations to avoid symmetry: hash(a,b) might be hash(a) XOR (hash(b) rotated by some bits). Simple XOR checksums fold data into a fixed-size value by XOR-ing successive blocks. Cryptographic hashes like SHA-256 use XOR in their compression functions alongside addition and rotation. The XOR operation is ideal for hashing because it preserves entropy: if either input has high entropy, the output maintains that randomness, ensuring good distribution of hash values.