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: 121012 \wedge 10. In binary, 12=110012 = 1100 and 10=101010 = 1010. Comparing: 11=01\wedge1=0, 10=11\wedge0=1, 01=10\wedge1=1, 00=00\wedge0=0. Result: 0110=60110 = 6.

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