Example – Filling a Bloom Filter

Published by Mario Oettler on

We have a 16 Bit long array (filter). At the start, it is empty (filled with 0).

We want to fill it with the following strings:

  • HOUSE
  • MOUSE
  • FLY

We use two different hash functions – SHA256 and SHA512.

You can use this hash calculator.

Hashcalculator


For each hash function, we take the first character of the hexadecimal representation of the hash output. In reality, bloom filters are larger and can handle more values. In real applications, the assignment of the hash to the bloom filter is different from out simple approach of taking only the first character.

For HOUSE, we have:

SHA-256: 3e83712a5c41abe11cf75a9ff41a66059e3bf823421bbc6d7bd3797e83786ed8 => 3

SHA-512: 2d25c894a14a1f10a13d4d5dd7dc1e433e80ca289be0b456fd2….. => 2

We set bit 2 and 3 to one.

Our bloom filter then looks like that:

Bloom filter with two bits set.

For MOUSE, we get:

SHA-256: 92297327e6e2fc4572c14a9d10c18d11e4c6a9701b1bcd2bcac9cfb64c871f9b => 9

SHA-512: f55f4bb60c938265225f438f14300ce7cfb103b24eae896f0f209a3d4a0e725a4c02… => f

If we add this to our previous bloom filter, it looks like that:

You can see that the additional bits 9 and f in the filter.

Our next string is FLY. We get:

SHA-256: f6f3918986164de5f5d6ecc5b56a4769396cd7a5da01b4b79e5f28b453be1960 => f

SHA-512: e56d246016937a656997868be22ff88683c3a5c5832d8b4e9e68559ad072aa8e6037… => e

You can see that bit f is already in the filter. But that doesn’t matter much. If a bit is already set, we keep it. Our filter then looks like that:

The more strings we add, the more bits are occupied.

If you want to play a bit with bloom filters, you can user this tool here:

Bloomfilter

Bloomfilter

Advanced Settings
Categories:

if()