ThisCalculator

Everyday

XOR Calculator: Bitwise Logic Explained

Updated · Standard formula, shown below

The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.

An XOR calculator performs the bitwise exclusive OR operation on two binary numbers, returning a result where each bit is 1 only if the corresponding bits of the inputs differ. You enter two integers (or binary strings), and the calculator outputs the XOR result in binary, decimal, and hexadecimal. This tool is essential for programmers, electronics enthusiasts, and students working with bit manipulation, checksums, or cryptography.

Formula

The XOR (exclusive OR) operation is defined bit by bit. For two bits x and y, the result is 1 if exactly one of them is 1, and 0 otherwise. The truth table is:

  • 0 XOR 0 = 0
  • 0 XOR 1 = 1
  • 1 XOR 0 = 1
  • 1 XOR 1 = 0

For multi-bit numbers, the operation is applied to each corresponding pair of bits (from least significant to most significant). In decimal notation, if you have two numbers A and B, the XOR result R is calculated by converting both to binary, performing the bitwise operation, and converting back. There is no simple arithmetic formula like addition or multiplication; it is purely a bitwise logical operation.

Worked Example

Using the default values: A = 12, B = 10.

  1. Convert 12 to binary: 1100
  2. Convert 10 to binary: 1010
  3. Align the bits (both are 4 bits): 1100 and 1010
  4. Apply XOR to each bit position:
    1 XOR 1 = 0
    1 XOR 0 = 1
    0 XOR 1 = 1
    0 XOR 0 = 0
  5. The binary result is 0110, which is 6 in decimal.
  6. In hexadecimal, 6 is 0x6.

So the calculator returns 6 (decimal), 0110 (binary), and 6 (hex).

When to Use an XOR Calculator

XOR is used in many areas of computing and electronics:

  • Checksums and error detection: Simple parity checks often use XOR to detect single-bit errors.
  • Cryptography: XOR is a building block for many ciphers, like the one-time pad.
  • Bit manipulation: Toggling specific bits in a register or flags variable.
  • Graphics and image processing: XOR blending can create special effects.
  • Networking: Calculating IP checksums.

If you are debugging code, verifying a checksum, or just learning how bitwise operations work, this calculator gives you a quick, error-free answer.

How to Read the Result

The primary output is the decimal result, which is what most programming languages will give you if you use the ^ operator. The binary and hexadecimal outputs are helpful for understanding why the result is what it is, especially when you are working with bit masks or hardware registers. For example, if you XOR a value with 0xFF, you flip the lowest 8 bits. The binary output lets you see exactly which bits were toggled.

Common Mistakes and Pitfalls

One common mistake is confusing XOR with OR. OR returns 1 if any bit is 1, while XOR returns 1 only if the bits are different. For example, 12 OR 10 = 14 (1110), while 12 XOR 10 = 6 (0110). Another pitfall is mixing up bit order or forgetting to pad numbers to the same length. Also, be careful with signed numbers: in many programming languages, XOR on signed integers uses two’s complement representation, which can yield negative results. This calculator treats input as unsigned for simplicity.

Limitations of the Formula

The XOR operation is defined for integers of arbitrary size, but in practice, hardware and programming languages use fixed-width integers (e.g., 32-bit or 64-bit). This calculator works with standard JavaScript numbers, which are 64-bit floating point, but bitwise operations in JavaScript are performed on 32-bit signed integers. That means for numbers larger than 2^31-1, the result may not match what you get in a language like Python (which has arbitrary precision). For most everyday use (numbers under 2 billion), it is accurate.

Frequently Asked Questions

What is the difference between XOR and OR?

OR returns 1 if at least one input bit is 1. XOR returns 1 only if exactly one input bit is 1. For example, 1 OR 1 = 1, but 1 XOR 1 = 0. This makes XOR useful for toggling bits, while OR is used for setting bits.

How do I calculate XOR of more than two numbers?

XOR is associative and commutative, so you can XOR them in any order. For example, (A XOR B) XOR C = A XOR (B XOR C). Simply enter the first two numbers, get the result, then XOR that result with the next number, and so on.

Can I use this calculator for binary strings?

Yes, you can enter binary numbers by converting them to decimal first. For example, binary 1010 is 10 in decimal. The calculator will output the result in binary as well, so you can verify your manual calculation.

Why does XOR sometimes produce a negative number in my code?

In many programming languages (like C, C++, Java, JavaScript), integers are signed and use two’s complement representation. If the most significant bit of the result is 1, the number is interpreted as negative. For example, 0xFFFFFFFF is -1 in a 32-bit signed integer. This calculator uses unsigned numbers, so it will show 4294967295 instead of -1.

Is XOR reversible?

Yes, XOR is its own inverse. If A XOR B = C, then C XOR B = A. This property is used in some simple encryption schemes and in swapping variables without a temporary variable.

What is the XOR of a number with itself?

Any number XOR itself equals 0, because every bit is identical, so each bit pair is 1 XOR 1 = 0. This is often used to clear a register or to check if two values are equal.

Related

Formula used
in a | First number (decimal) | - | 12 | 0
in b | Second number (decimal) | - | 10 | 0
out xor_result | XOR result (decimal) | - | a ^ b | 0
out xor_bin | XOR result (binary) | - | dec2bin(a ^ b) | 0
out xor_hex | XOR result (hexadecimal) | - | dec2hex(a ^ b) | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.