Bitwise XOR Calculator

Calculate A ^ B online and identify the bit positions where the two input values are different.

Number A
Dec46 Hex0x2E
Number B
Dec74 Hex0x4A

XOR Result

XOR (^)
Dec0
Hex0x00
Bin00000000

Bit-by-Bit Visualization

1 Active bit 0 Diff bit 0 Included zero 0 Leading zero

XOR Truth Table

Click a row to load values into A and B, or inspect how each gate behaves.

Input AInput BXOR
000
011
101
110
Click a truth table row to test those values.

How the Bitwise XOR Calculator Works

A bitwise XOR calculator returns 1 when two compared bits are different. XOR is useful for toggles, parity checks, checksums, and difference masks.

XOR Rule

The output bit is 1 when one input is 1 and the other input is 0.

Difference Mask

XOR highlights exactly where two binary values differ.

Toggle Bits

XOR with a mask flips selected bits while leaving other positions unchanged.

Parity Checks

Many parity and checksum routines use XOR because repeated equal bits cancel out.

Bitwise XOR Calculation Steps

Use these steps to calculate XOR without guessing.

Align Both Values

  • Convert A and B to binary.
  • Pad the shorter value with leading zeros.
  • Keep columns aligned from right to left.

Mark Different Bits

  • Write 1 when the two bits are different.
  • Write 0 when the two bits match.
  • Continue through every bit column.

Convert the Output

  • Read the binary result.
  • Convert it to decimal or hex.
  • Use the value as a toggle or difference mask.

Bitwise XOR Examples

XOR examples show where two values differ.

InputOperationResultUse
101110 ^ 1001010 XOR 1100100 Difference mask
1111 ^ 0101 XOR 1010 Toggle selected bits
0x2E ^ 0x4A XOR 0x64 Hex difference
46 ^ 74 XOR 100 Decimal integer result

Why Use This Bitwise XOR Calculator?

Use this XOR calculator to inspect binary differences, toggles, and parity behavior.

Difference Highlighting

The bit grid marks every position where A and B differ, which is exactly where XOR returns 1.

Hex Nibble Results

Check XOR one hex digit at a time, useful for checksums and key streams.

Width Control

Pick 8, 16, 32, or 64 bits so toggles and all-ones masks match your data type.

Equality Check

A result of 0 confirms the two inputs are identical, bit for bit.

Practical Uses for Bitwise XOR

XOR is especially helpful when equal bits should cancel and different bits should stand out.

Toggle Bits

state ^= MASK flips selected bits on and off.

Parity and RAID

XOR of data blocks produces parity that can rebuild a lost block.

Stream Ciphers

Ciphertext is plaintext XOR keystream, and the same operation decrypts it.

Hamming Distance

The number of 1s in a ^ b counts the bit positions where two values differ.

What the Bitwise XOR Operator Does

Bitwise XOR (exclusive OR, written ^ in most languages) outputs 1 when the two input bits are different and 0 when they are the same.

ABA ^ B
000
011
101
110

Read the table one row at a time and a second interpretation appears: when B is 0, the output equals A; when B is 1, the output is A flipped. XOR is therefore a controlled inverter. The second operand chooses which bits of the first operand get toggled.

Worked Example: 46 ^ 74 Step by Step

  00101110   (46, 0x2E)
^ 01001010   (74, 0x4A)
  --------
  01100100   (100, 0x64)

Bits 1 and 3 are set in both inputs, so they cancel to 0. Bits 2, 5, and 6 are set in only one input, so they survive: 4 + 32 + 64 = 100. In hex: 2 ^ 4 = 6 and E ^ A = 4, giving 0x64.

XOR is closely related to addition without carries. In fact A + B = (A ^ B) + 2 × (A & B). With the example, 100 + 2 × 10 = 120, which is 46 + 74.

The Algebra That Makes XOR Useful

Four properties explain almost every XOR trick:

  1. Self-inverse: x ^ x = 0. Any value XORed with itself disappears.
  2. Identity: x ^ 0 = x. XOR with zero changes nothing.
  3. Commutative and associative: order and grouping don’t matter, so a ^ b ^ a = b.
  4. Reversible: if c = a ^ k, then c ^ k = a. Applying the same key twice restores the original.

Property 4 is why XOR appears in encryption, checksums, and RAID parity. Property 1 and 3 together power the “find the unique element” interview question below.

Toggling Bits and Flipping States

To flip a single flag, XOR with a mask that has 1 in that position:

#define LED_PIN (1u << 5)
port ^= LED_PIN;      /* LED on → off, or off → on */

To flip every bit in a fixed-width field, XOR with an all-ones mask. For 8 bits, x ^ 0xFF gives the same result as an 8-bit NOT. Unlike ~x, it never sets bits outside the field, which avoids surprises with larger integer types.

XOR also compares two values bit by bit. a ^ b is 0 exactly when a == b, and the number of 1 bits in a ^ b is the Hamming distance, the count of positions where the two values differ. Error-correcting codes and similarity hashing rely on that count.

Classic XOR Tricks: Swap and Find the Unique Number

Swap without a temporary variable:

a ^= b;   /* a = a ^ b          */
b ^= a;   /* b = b ^ a ^ b = a  */
a ^= b;   /* a = a ^ b ^ a = b  */

It is a good illustration of the algebra, but modern compilers produce faster code with a normal temporary variable, and the trick fails if a and b refer to the same memory location (it zeroes it). Treat it as a puzzle, not an optimisation.

Find the element that appears once: if every number in a list appears twice except one, XOR all of them. Pairs cancel to zero and the single value remains.

from functools import reduce
from operator import xor

nums = [4, 1, 2, 1, 2]
print(reduce(xor, nums))   # 4

This runs in one pass with constant memory, which is why it is a favourite coding-interview question.

XOR in Parity, Checksums, and Encryption

  • Parity bit: XOR all the bits of a byte together. The result is 1 if the byte contains an odd number of 1 bits. Serial links and memory systems use parity to detect single-bit errors.
  • RAID 5: the parity block is the XOR of the data blocks. If one disk fails, XOR the surviving blocks with the parity block to rebuild the lost data.
  • Stream ciphers and the one-time pad: ciphertext is plaintext ^ keystream, and decryption is the same operation. XOR on its own is only secure when the key is truly random, as long as the message, and never reused. A repeating short key, such as XORing a file with 0x5A, is easy to break.

Bitwise XOR in Python and JavaScript

print(46 ^ 74)            # 100
print(bin(0b1010 ^ 0b0110))  # 0b1100
print(bin(46 ^ 74).count("1"))  # Hamming distance: 3
console.log(46 ^ 74);     // 100
console.log(5 ^ 5);       // 0
// Note: ^ is XOR, not exponentiation. Use ** for powers.
console.log(2 ^ 3);       // 1, not 8

Common Mistakes With Bitwise XOR

  • Using ^ for powers. In C, Java, Python, and JavaScript, 2 ^ 3 is 1. Exponentiation is ** or pow().
  • Confusing XOR with OR. OR gives 1 | 1 = 1; XOR gives 1 ^ 1 = 0. Use OR to set bits and XOR to toggle them.
  • Assuming XOR is encryption. A fixed or repeating XOR key only obscures data. Use a vetted cipher for real security.
  • Width problems with NOT-style flips. x ^ 0xFF only flips the low 8 bits. Pick a mask that matches your data width.

Frequently Asked Questions

Find answers to common questions about bitwise operations and this online tool.

What does bitwise XOR do?

XOR (exclusive OR) returns 1 in each bit position where the two inputs are different and 0 where they are the same. For example, 46 ^ 74 = 100.

Is ^ the power operator?

Not in C, C++, Java, Python, or JavaScript, where ^ is bitwise XOR. 2 ^ 3 is 1, not 8. Use ** or pow() for exponents.

Why is a ^ a always 0?

Every bit is compared with an identical bit, so no position differs and every output bit is 0. This self-inverse property powers XOR swap and the find-the-unique-number trick.

How do you toggle a bit with XOR?

Use value ^= (1 << n). If bit n is 1 it becomes 0, and if it is 0 it becomes 1. Other bits are unchanged.

Is XOR encryption secure?

Only as a one-time pad, where the key is truly random, as long as the message, and never reused. A short or repeating XOR key is easy to break and should not be used to protect data.

How does XOR find the number that appears once?

If every value appears twice except one, XOR all of them together. Pairs cancel to 0 because x ^ x = 0, leaving only the single value.