Skip to content

Instantly share code, notes, and snippets.

@aristus
Created January 21, 2014 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aristus/8541397 to your computer and use it in GitHub Desktop.
Save aristus/8541397 to your computer and use it in GitHub Desktop.
from hashlib import md5
bloom = 0b0
num_bits = 512
def add_to_bloom(s):
global bloom
# int representation of the hashed value
hash = int(md5(s.encode('utf8')).hexdigest(), 16)
# modulo that number against the length of the bloom filter
m = hash % num_bits
# flip the mth bit in the filter!
bit = 1 << m
bloom |= bit
return m
print bloom
print add_to_bloom('derp')
print add_to_bloom('foo')
print bloom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment