Skip to content

Instantly share code, notes, and snippets.

@aristus
Created January 21, 2014 14:47
Show Gist options
  • Select an option

  • Save aristus/8541397 to your computer and use it in GitHub Desktop.

Select an option

Save aristus/8541397 to your computer and use it in GitHub Desktop.

Revisions

  1. aristus created this gist Jan 21, 2014.
    27 changes: 27 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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