Utility Coder
Back to Blog
calculator

XOR Calculator

Andy Pham
xor calculator, xor calculator, xor calculator online

Ready to try XOR Calculator?

Access the free online tool now - no registration required

Open Tool

XOR Calculator - Complete Guide

Bitwise XOR (exclusive OR) calculator compares bits and outputs 1 only when inputs differ. XOR has unique properties: self-inverse, no carry, and useful for toggling, swapping, checksums, and simple encryption.

How It Works

Input two numbers. Calculator converts to binary and XORs each bit position. Output is 1 if inputs differ (0^1=1, 1^0=1), 0 if same (0^0=0, 1^1=0). Key property: A XOR B XOR B = A.

Technical Details

XOR truth table: 0^0=0, 0^1=1, 1^0=1, 1^1=0. Properties: commutative (A^B = B^A), associative ((A^B)^C = A^(B^C)), self-inverse (A^A=0), identity (A^0=A). Used in: encryption, checksums, swapping, bit toggling.

Common Use Cases

  1. Simple Encryption: XOR with key to encrypt, XOR again to decrypt.
    • Example: plaintext XOR key = cipher, cipher XOR key = plaintext
  2. Checksum Calculation: XOR all bytes for simple error detection.
  3. Bit Toggling: Flip specific bits without conditionals.
    • Example: value ^ mask toggles masked bits
  4. Swap Without Temp: a ^= b; b ^= a; a ^= b; swaps a and b.

Code Examples

Bitwise XOR Operations

// Basic XOR
console.log(12 ^ 10);  // 6
// 1100 (12)
// 1010 (10)
// ----
// 0110 (6)

// Toggle bits
const toggleBit = (n, pos) => n ^ (1 << pos);
toggleBit(0b1010, 1);  // 0b1000 (toggle bit 1 off)
toggleBit(0b1000, 1);  // 0b1010 (toggle bit 1 on)

// Simple XOR encryption/decryption
const xorEncrypt = (text, key) =>
  text.split('').map((c, i) =>
    String.fromCharCode(c.charCodeAt(0) ^ key.charCodeAt(i % key.length))
  ).join('');

const encrypted = xorEncrypt('Hello', 'key');
const decrypted = xorEncrypt(encrypted, 'key');  // "Hello"

// Find unique element (all others appear twice)
const findUnique = arr => arr.reduce((a, b) => a ^ b, 0);
findUnique([1, 2, 1, 3, 2]);  // 3

XOR outputs 1 when bits differ. Self-inverse property enables encryption, swapping, and finding unique elements.

Tips & Best Practices

  • A ^ A = 0 (self-canceling)
  • A ^ 0 = A (identity)
  • Use for toggling bits
  • Simple encryption (not secure alone)
  • Find unique element in pairs

Common Mistakes to Avoid

  • Using XOR encryption without proper key management
  • Confusing with OR (they differ on 1,1)
  • Integer overflow in swap trick
  • Not understanding self-inverse property

When to Use This Tool

Use XOR for bit toggling, simple checksums, finding unique elements, and as building block for encryption. Combine with other bitwise ops for complex bit manipulation.

Frequently Asked Questions

Why is XOR used in encryption?

XOR is its own inverse: encrypt(decrypt(x)) = x. Simple XOR cipher: ciphertext = plaintext ^ key, plaintext = ciphertext ^ key. Not secure alone (key reuse is vulnerable) but used in stream ciphers.

How does XOR swap work?

a ^= b; b ^= a; a ^= b; works because: (1) a becomes a^b, (2) b becomes (a^b)^b = a, (3) a becomes (a^b)^a = b. No temp variable needed, but less readable than temp swap.

What is parity checking with XOR?

XOR all bits/bytes together. Result has odd parity (1) if odd number of 1-bits, even (0) if even. Used in RAID, error detection. XOR of data and parity = 0 means no error.

Why is A XOR A = 0?

XOR outputs 0 when bits are same. Every bit of A compared with itself is same, so result is all zeros. This property enables: finding unique element (XOR all, pairs cancel), checksum validation.

Get Started

Ready to try XOR Calculator? Use the tool now - it's free, fast, and works right in your browser.


Last updated: June 27, 2026

Start using XOR Calculator

Free, fast, and privacy-focused - try it now

Launch Tool