Math Fundamentals18 sections · 814 units
Open in Course

Bitwise XOR (^)

Bits differ

The XOR operator (^) compares two numbers bit by bit. The result has a 1 when the bits differ, and 0 when they're the same.

Example: 12∧1012 \wedge 10. In binary, 12=110012 = 1100 and 10=101010 = 1010. Comparing: 1∧1=01\wedge1=0, 1∧0=11\wedge0=1, 0∧1=10\wedge1=1, 0∧0=00\wedge0=0. Result: 0110=60110 = 6.

XOR has a magic property: x∧x=0x \wedge x = 0 and x∧0=xx \wedge 0 = x. This makes XOR perfect for finding unique elements, toggling bits, and swapping values without a temporary variable.