Bitwise XOR in C – Why 5 ^ 3 Is 6 (and ^ Is Not Power)

Bitwise XOR — exclusive OR — keeps a bit only where the two operands differ. It’s the strangest of the bitwise trio at first sight, and the most magical once you know its two special properties. This question from our C Programming Quiz App starts with the arithmetic.

The Quiz Question

int a = 5;
printf("%d", a ^ 3);

What is printed by this code?

  1. 6
  2. 7
  3. 15
  4. 2

The Correct Answer: 6

Line up the binary and XOR each column — a result bit is 1 where the inputs disagree, 0 where they match:

  5  =  1 0 1
  3  =  0 1 1
  ─────────── ^
         1 1 0  =  6

Top bit: 1 vs 0 → differ → 1. Middle: 0 vs 1 → differ → 1. Bottom: 1 vs 1 → same → 0. Result 110 = 6. Verified on gcc 13.3 and Apple clang 21, warning-free:

$ gcc -Wall -Wextra xor.c && ./a.out
6

Why Each Wrong Answer Is Wrong

Why not 7?

7 (111) is bitwise OR: 5 | 3 keeps a bit if either input has it, including where both do. XOR is OR’s picky sibling — it rejects the positions where both inputs are 1. That bottom bit (1 in both 5 and 3) is exactly the difference between 7 and 6 here.

Why not 15?

15 is 5 × 3 — plain multiplication. The caret in C is XOR, not exponentiation either (there is no power operator in C; that’s pow() from <math.h>). 2 ^ 3 is 1, not 8 — a bug that has bitten many a newcomer.

Why not 2?

2 is 5 - 3 — subtraction. XOR does behave a little like “difference detection”, but on bits, not magnitudes: it flags which positions differ, it doesn’t subtract values.

XOR’s Two Superpowers

Anything XOR itself is zero, and XOR undoes itself. Both verified in our test run:

printf("%d\n", a ^ a);         /* 0 — identical bits everywhere      */
printf("%d\n", (a ^ 3) ^ 3);   /* 5 — XOR by 3 twice = original      */

The self-inverse property is why XOR shows up everywhere serious bit work happens: toggling flags (flags ^= FLAG flips one bit on/off), simple ciphers (XOR with a key encrypts, XOR with the same key decrypts), parity and checksums (RAID reconstructs a lost disk by XORing the others), and the interview classic — finding the number that appears an odd number of times by XORing the whole array, since every pair cancels to 0.

And yes, the famous XOR swap (x ^= y; y ^= x; x ^= y;) really works without a temporary — but on modern compilers a plain three-line swap with a temp is just as fast and far clearer. Know the trick for interviews; don’t ship it.

Frequently Asked Questions

Is ^ the power operator in C?

No — C has no exponentiation operator. ^ is bitwise XOR: 2 ^ 3 is 1, not 8. Use pow() from <math.h> for powers.

What is x ^ x in C?

Always 0 — every bit matches itself, and XOR outputs 0 where bits agree. This cancellation is the basis of parity tricks and the odd-one-out interview problem.

How does XOR encryption work?

XOR is self-inverse: (m ^ k) ^ k == m. XORing a message with a key scrambles it; XORing the result with the same key restores it. (Real cryptography needs much more than this, but the XOR core is genuine — it’s how one-time pads work.)

Related Reading

Recommended Books

This question is #62 in the C Programming Quiz App — 155 questions with explanations covering operators, pointers, memory, and more.
Download on Google Play →

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>