Bits, Probability & Poisoned Wine

Interactive visualizer for four classic interview problems.

x x - 1 AND

A power of 2 has exactly one set bit. Subtracting 1 turns that bit off and all lower zeros into ones, so x & (x - 1) becomes 0.

A left shift by 3 multiplies by 2³ = 8. Therefore (x << 3) - x = 8x - x = 7x.

Bit-by-bit comparison
Why it works

A sequence of fair coin flips creates a random binary number U = 0.c₁c₂c₃…, uniform on [0,1).

Return True exactly when U < p, so:

P(U < p) = p

For a finite binary expansion, treat p as continuing with zeros forever. If all supplied bits tie, the result is False except for the probability-zero event of exact equality.

Bottle

After 18 hours, dead mice represent 1-bits. Reading the pattern as a binary number identifies the poisoned bottle.

Why 10 mice?

Each mouse gives one binary outcome: alive = 0, dead = 1.

2¹⁰ = 1024 unique patterns, enough for 1000 bottles.