# Use of hash code in integer hashing

**URL:** https://discourse.julialang.org/t/use-of-hash-code-in-integer-hashing/82068
**Category:** Internals & Design
**Created:** [June 1, 2022, 5:44pm UTC](https://discourse.julialang.org/t/use-of-hash-code-in-integer-hashing/82068 "2022-06-01T17:44:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [June 1, 2022, 5:44pm UTC](https://discourse.julialang.org/t/use-of-hash-code-in-integer-hashing/82068/1 "2022-06-01T17:44:19Z")

</div>

I’m curious about the design of the integer hashing functions. Someone in my company coming from Python was annoyed to discover that the second argument to `hash` doesn’t produce very different results for incremented values of `h`, and there’s at least one package he was trying to use (bloom filters in Probably.jl) that assumes they would be.

```julia
julia> hash(0x000000000796a326, UInt64(0))
0x574cf859055c7b75

julia> hash(0x000000000796a326, UInt64(1))
0x574cf859055c7b72

julia> hash(0x000000000796a326, UInt64(2))
0x574cf859055c7b6f

julia> hash(0x000000000796a326, UInt64(3))
0x574cf859055c7b6c

```

```julia
# from hashing.jl:
hash(x::Int64, h::UInt) = hash_uint64(bitcast(UInt64, x)) - 3h
hash(x::UInt64, h::UInt) = hash_uint64(x) - 3h

```

I’d like to be able to explain this design decision to him, and perhaps expand the documentation to warn about using the hash function that way.

---

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [June 1, 2022, 9:32pm UTC](https://discourse.julialang.org/t/use-of-hash-code-in-integer-hashing/82068/2 "2022-06-01T21:32:40Z")

</div>

On pondering, I’m thinking the intent is that `h` is supposed to be the output of a previous hash function call? (hence, calling it a hash code)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [June 1, 2022, 9:36pm UTC](https://discourse.julialang.org/t/use-of-hash-code-in-integer-hashing/82068/3 "2022-06-01T21:36:34Z")

</div>

> [@BioTurboNick](#):
>
> I’m thinking the intent is that `h` is supposed to be the output of a previous hash function

Yes, it’s for mixing multiple hashes together.

```julia
julia> hash(0x000000000796a326, hash(0))
0xefde128c35091bc5

julia> hash(0x000000000796a326, hash(1))
0x43ed831bde9d910b

```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [June 2, 2022, 5:49pm UTC](https://discourse.julialang.org/t/use-of-hash-code-in-integer-hashing/82068/4 "2022-06-02T17:49:54Z")

</div>

It kind of seems like the `- 3h` could go inside the call to `hash_uint64` Instead of outside. You want to make sure the function is asymmetrical in the two arguments but the factor of `-3` ensures that already. The only down side I can see is that it could make it easier to craft an input that interacts badly with given hashes. Would be good to look at the history of this definition.
