XOR Calculator
Free XOR Calculator for number systems. Enter values to get step-by-step solutions with formulas and graphs. Includes formulas and worked examples.
Reviewed for accuracy by Manoj Kumar, Mathematics Educator
XOR Calculator
Calculator
Adjust values & calculateEnter your values below. Every result is computed in your browser — no data is sent to any server.
Formula: A XOR B: output 1 when bits differ, 0 when same
Worked example — 42 XOR 27 = 49 (0x31) | Hamming distance: 4
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 27 in binary: 00011011 XOR operation (1 where bits differ): 00101010 00011011 -------- 00110001 = 49 Hamming 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) Step 1: A = A XOR B = 15 XOR 9 = 6 (00000110) Step 2: B = A XOR B = 6 XOR 9 = 15 (00001111) Step 3: A = A XOR B = 6 XOR 15 = 9 (00001001) Result: 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 is the XOR (exclusive or) operation and how does it work?
XOR (exclusive or) is a logical bitwise operation that outputs 1 (true) when the inputs are different and 0 (false) when the inputs are the same. For single bits: 0 XOR 0 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1, 1 XOR 1 = 0. When applied to multi-bit numbers, XOR operates on each pair of corresponding bits independently. For example, 42 XOR 27: 42 in binary is 00101010 and 27 is 00011011, so XOR gives 00110001 = 49. The XOR operation is fundamental in computer science, used in error detection, encryption, hash functions, and swap algorithms. It is both commutative (A XOR B = B XOR A) and associative.
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 can you swap two variables using XOR without a temporary variable?
The XOR swap algorithm exchanges two values without needing extra memory: Step 1: A = A XOR B. Step 2: B = A XOR B (which is now original A XOR B XOR B = original A). Step 3: A = A XOR B (which is now original A XOR B XOR original A = original B). After these three operations, A and B have been swapped. For example, starting with A=42 and B=27: After step 1, A=49. After step 2, B=42 (the original A). After step 3, A=27 (the original B). While elegant, this trick is rarely used in modern programming because compiler-optimized temporary variables are faster on modern processors, and the XOR swap fails if both variables reference the same memory location.
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.
What are XOR-based algorithms in competitive programming?
XOR is a powerful tool in competitive programming problems. Finding a single number that appears an odd number of times in an array: XOR all elements (duplicates cancel out). Finding two unique numbers among duplicates: XOR all elements, use a set bit to partition, then XOR each partition. The XOR maximum problem uses a trie to find the pair of numbers with maximum XOR. XOR basis (linear basis) finds the maximum XOR subset or determines if a target XOR is achievable. Nimber theory uses XOR for optimal strategy in Nim games. Prefix XOR arrays enable O(1) range XOR queries. Gray code generation uses XOR with bit shifting to produce sequences where adjacent codes differ by exactly one bit.
How does XOR relate to binary addition and parity?
XOR is identical to binary addition without carry: 0+0=0, 0+1=1, 1+0=1, and 1+1=10 (but XOR gives just the 0, ignoring the carry). This is why a half adder circuit uses XOR for the sum bit and AND for the carry bit. XOR also computes the parity of a set of bits: XOR-ing all bits together yields 1 if the count of 1-bits is odd, and 0 if even. This parity property extends to any number of inputs and is the basis for parity-based error detection. In modular arithmetic, XOR corresponds to addition modulo 2 for each bit position, which connects it to finite field arithmetic over GF(2), the mathematical foundation of coding theory.
References
Reviewed for accuracy by Manoj Kumar, Mathematics Educator · Editorial policy
Related Calculators
🧮Annulus Area Calculator
Calculate annulus area with inputs, formulas, and instant results.
🧮Area Calculator
Calculate area with inputs, formulas, and instant results.
🧮Area of a Rectangle Calculator
Calculate the area, perimeter, and diagonal of a rectangle. Find missing sides from known area. Convert between metric and imperial area units.
🧮Area of Crescent Calculator
Calculate area of crescent with inputs, formulas, and instant results.
🧮Center of Mass Calculator
Calculate center of mass with inputs, formulas, and instant results.
🧮Centroid Calculator
Calculate centroid with inputs, formulas, and instant results.
🧮Chord Length Calculator
Calculate chord length with inputs, formulas, and instant results.
🧮Conic Sections Calculator
Calculate conic sections with inputs, formulas, and instant results.