← Back to Games

🔀 Bit Switch Chamber

Toggle the ith bit - flip between states with XOR!

⚡ Toggle Control
n ^ (1 << i) → Toggle bit i
Original Number (n) 0
Mask (1 << i) 0
Result (n ^ mask) 0
Bit 0: 0 1

📚 Toggle the ith Bit

To toggle (flip) the ith bit of a number, use XOR with a mask:

result = n ^ (1 << i)

// XOR Truth Table:
// 0 ^ 1 = 1 (turns ON)
// 1 ^ 1 = 0 (turns OFF)

// Example: Toggle bit 2 of 85 (01010101)
// 1 << 2 = 00000100
// XOR -----------
// 81 = 01010001

XOR with 1 always flips the bit. This is perfect for toggles!