# Why does my hash function return a different value to the built in hash function?

**URL:** https://discourse.julialang.org/t/why-does-my-hash-function-return-a-different-value-to-the-built-in-hash-function/77973
**Category:** General Usage
**Created:** [March 16, 2022, 2:18pm UTC](https://discourse.julialang.org/t/why-does-my-hash-function-return-a-different-value-to-the-built-in-hash-function/77973 "2022-03-16T14:18:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Jon\_Barker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_barker/32/18997_2.png) [@Jon\_Barker](https://discourse.julialang.org/u/Jon_Barker)
#### Post date: [March 16, 2022, 2:18pm UTC](https://discourse.julialang.org/t/why-does-my-hash-function-return-a-different-value-to-the-built-in-hash-function/77973/1 "2022-03-16T14:18:35Z")

</div>

I copied the `hash` function code from here:

> <https://github.com/JuliaLang/julia/blob/v1.7.0/base/hashing.jl>

And ran it under J1.7 but I get different results. Why?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/3/736d24aa3d7e7860b004a610b5011a6a0d9a808f.png)

---

<div class="post-metadata">

### Author: ![Jon\_Barker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_barker/32/18997_2.png) [@Jon\_Barker](https://discourse.julialang.org/u/Jon_Barker)
#### Post date: [March 16, 2022, 2:24pm UTC](https://discourse.julialang.org/t/why-does-my-hash-function-return-a-different-value-to-the-built-in-hash-function/77973/3 "2022-03-16T14:24:58Z")

</div>

> [@lawless-m](#):
>
> `hash2(x::Any) = hash(x, zero(UInt))`

Your second line - that should be:

`hash2(x::Any) = hash2(x, zero(UInt))`

?

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [March 16, 2022, 2:27pm UTC](https://discourse.julialang.org/t/why-does-my-hash-function-return-a-different-value-to-the-built-in-hash-function/77973/5 "2022-03-16T14:27:44Z")

</div>

Ok, let me rephrase that  
I get the same

```julia
julia> hash2((1,2))
0xc04cd80fdd457cea

```

---

<div class="post-metadata">

### Author: ![Jon\_Barker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_barker/32/18997_2.png) [@Jon\_Barker](https://discourse.julialang.org/u/Jon_Barker)
#### Post date: [March 16, 2022, 2:30pm UTC](https://discourse.julialang.org/t/why-does-my-hash-function-return-a-different-value-to-the-built-in-hash-function/77973/6 "2022-03-16T14:30:29Z")

</div>

Still no clue why I don’t get that then:

```julia
hash2(x::Any) = hash2(x, zero(UInt))
hash2(@nospecialize(x), h::UInt) = Base.hash_uint(3h - objectid(x))
hash((1,2))==hash2((1,2))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/e/4e058b5dd6a537f981a58476da6a8458cbe05fc0.png)

---

<div class="post-metadata">

### Author: ![Jon\_Barker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_barker/32/18997_2.png) [@Jon\_Barker](https://discourse.julialang.org/u/Jon_Barker)
#### Post date: [March 16, 2022, 2:31pm UTC](https://discourse.julialang.org/t/why-does-my-hash-function-return-a-different-value-to-the-built-in-hash-function/77973/7 "2022-03-16T14:31:48Z")

</div>

Ah i saw you edited your post - OK good I’m not going crazy.  
Anyone shed some light on this?

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [March 16, 2022, 2:32pm UTC](https://discourse.julialang.org/t/why-does-my-hash-function-return-a-different-value-to-the-built-in-hash-function/77973/8 "2022-03-16T14:32:16Z")

</div>

There’s a special hash function for tuples:

```julia
julia> @which hash((1,2), zero(UInt))
hash(t::Tuple, h::UInt64) in Base at tuple.jl:417

```

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [March 16, 2022, 2:32pm UTC](https://discourse.julialang.org/t/why-does-my-hash-function-return-a-different-value-to-the-built-in-hash-function/77973/9 "2022-03-16T14:32:55Z")

</div>

I even took hashing.jl, replaced all the `hash(` with `hash2(` and got the same result

---

<div class="post-metadata">

### Author: ![Jon\_Barker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_barker/32/18997_2.png) [@Jon\_Barker](https://discourse.julialang.org/u/Jon_Barker)
#### Post date: [March 16, 2022, 2:33pm UTC](https://discourse.julialang.org/t/why-does-my-hash-function-return-a-different-value-to-the-built-in-hash-function/77973/10 "2022-03-16T14:33:41Z")

</div>

`@which`  
Everytime I post here I learn of a new helpful macro. Thanks!
