Bitwise XOR (eXclusive OR)

The XOR bitwise operation (or exclusive or) takes two sets of bits, and for each pair (the two bits at the same index in each bit set) returns 1 only if one but not both of the bits is 1. Otherwise, it returns 0.

1 ^ 1 → 0 1 ^ 0 → 1 0 ^ 1 → 1 0 ^ 0 → 0

Think of it like a bag of chips where only one hand can fit in at a time. If no one reaches for chips, no one gets chips, and if both people reach for chips, they can't fit and no one gets chips either!

When performing XOR on two integers, only digit columns used by one but not both integers remain:

5 ^ 6 # gives 3 # At the bit level: # 0101 (5) # ^ 0110 (6) # = 0011 (3)

See also:

Pass Your Interviews with My FREE 7-Day Crash Course

I'll teach you the right way of thinking for breaking down tricky algorithmic coding interview questions you've never seen before.

No prior computer science training necessary—I'll get you up to speed quickly, skipping all the overly academic stuff.

No spam. One-click unsubscribe if you hate it.

Psst. Pass it on.

. . .