Adding words
A Bloom filter remembers which words it has seen, using very little memory. It is an array of m bits, all 0 to start (here m = 12), plus k hash functions (here k = 3). A hash function turns a word into a number.
spots("cat") runs the word through the 3 hash functions. Each gives a number from 0 to 11: a spot in the array. For cat they are 1, 5, 6. The same word always gives the same spots.
add() sets the bit at each spot to 1: bits 1, 5, 6. That is all an insert does. The word itself is not stored anywhere, so the filter stays the same size however long the words are.
dog's spots are 0, 5, 7. Bit 5 is already 1 from cat, so it just stays 1. Words share bits. Now 5 of the 12 bits are 1, and nothing records which word set which bit.
Asking questions
has() looks at the same spots: 1, 5, 6. All three are 1, so it returns True. That means "maybe": the word was probably added. An added word always finds its own ones, so a Bloom filter never wrongly says no.
fox's spots are 6, 11, 9. Bit 6 is 1, but bit 11 is 0, so has() returns False at once and never checks bit 9. A single 0 proves fox was never added: if it had been, that bit would be 1.
dove was never added, but its spots 1, 6, 7 are all 1: bits 1 and 6 came from cat, bit 7 from dog. has() returns True. This wrong "maybe" is a false positive, the price paid for storing only bits.
The limits
With 5 of 12 bits set, a new word lands on three ones about (5/12)³ ≈ 7% of the time. In general p ≈ (1 − e^(−kn/m))^k for n words. With 10 bits per word and k = 7, p is under 1%.
Try to remove cat by clearing bits 1, 5, 6. Bit 5 was dog's too, so has("dog") now finds a 0 and says no: a false negative. That breaks the filter's one promise, so a plain Bloom filter cannot delete.
What happens
;; step 1/9 · bit-array
bits = [0] * M M = 12
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
K = 3 hash functions, numbered 0, 1, 2
it stores no words, only bits
The formula p ≈ (1 − e^(−kn/m))^k assumes uniform, independent hashes (Broder and Mitzenmacher, 2004). It is lowest at k = (m/n) ln 2 hash functions, so about 10 bits per word give under 1% false positives, however long the words are. A Bloom filter has false positives but no false negatives: if has(w) returns False, w was certainly never added. Bloom introduced the idea in 1970 to save memory when a few wrong "maybe" answers are acceptable.