# Casting Int64 to UInt64

**URL:** <https://discourse.julialang.org/t/casting-int64-to-uint64/33856>\
**Category:** General Usage\
**Created:** [January 27, 2020, 3:28pm UTC](https://discourse.julialang.org/t/casting-int64-to-uint64/33856 "2020-01-27T15:28:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niclas\_Wiberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclas_wiberg/32/19063_2.png) [@Niclas\_Wiberg](https://discourse.julialang.org/u/Niclas_Wiberg)\
**Post date:** [January 27, 2020, 3:28pm UTC](https://discourse.julialang.org/t/casting-int64-to-uint64/33856/1 "2020-01-27T15:28:38Z")

</div>

What is the best way to cast an Int64 to an UInt64, preserving the binary representation?

For instance, given -1 as input I would like to get 0xffffffffffffffff. Given -256 as input I would like to get 0xffffffffffffff00.

Simply doing UInt64(-1) gives an error because UInt64 expects nonnegative input. Naively doing UInt64(-1 + (1\<\<64)) gives the same result, since 1\<\<64 evaluates to zero.

One possibility is to define f(x) = UInt64(x + (UInt128(1) \<\< 64)). However it seems a bit weird to have to resort to 128-bit arithmetic just to perform a type cast.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [January 27, 2020, 3:30pm UTC](https://discourse.julialang.org/t/casting-int64-to-uint64/33856/2 "2020-01-27T15:30:11Z")

</div>

```julia
julia> reinterpret(UInt64, Int64(-1))
0xffffffffffffffff

julia> reinterpret(UInt64, Int64(-256))
0xffffffffffffff00

julia> 

```

---

<div class="post-metadata">

**Author:** ![Niclas\_Wiberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclas_wiberg/32/19063_2.png) [@Niclas\_Wiberg](https://discourse.julialang.org/u/Niclas_Wiberg)\
**Post date:** [January 27, 2020, 3:30pm UTC](https://discourse.julialang.org/t/casting-int64-to-uint64/33856/3 "2020-01-27T15:30:37Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 27, 2020, 3:42pm UTC](https://discourse.julialang.org/t/casting-int64-to-uint64/33856/4 "2020-01-27T15:42:31Z")

</div>

`x % T` is shorter and more forgiving unless you want to make sure only `Int64` were allowed as input types.

---

<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:** [January 27, 2020, 3:55pm UTC](https://discourse.julialang.org/t/casting-int64-to-uint64/33856/5 "2020-01-27T15:55:04Z")

</div>

I would strongly recommend the `x % T` version of this over the `reinterpret` version. The reason is that `x % T` is a well-defined operation independent of the representation of the values, it’s just one that happens to be entirely free for `Int64` and `UInt64`. It’s better to express the behavior you want than rely on the details of the representation.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [January 27, 2020, 5:06pm UTC](https://discourse.julialang.org/t/casting-int64-to-uint64/33856/6 "2020-01-27T17:06:52Z")

</div>

Thanks for adding the rationale.
