# Can you twiddle bits in a Float64 variable?

**URL:** <https://discourse.julialang.org/t/can-you-twiddle-bits-in-a-float64-variable/121271>\
**Category:** New to Julia\
**Tags:** internals\
**Created:** [October 14, 2024, 4:38am UTC](https://discourse.julialang.org/t/can-you-twiddle-bits-in-a-float64-variable/121271 "2024-10-14T04:38:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [October 14, 2024, 4:38am UTC](https://discourse.julialang.org/t/can-you-twiddle-bits-in-a-float64-variable/121271/1 "2024-10-14T04:38:33Z")

</div>

Can you twiddle bits in a Float64 variable?

Can you have x = 123.456  
then type cast x into UInt64  
Twiddle its bits  
then type cast it back to Float64 to get a different Float64 number?

Just wondering.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 14, 2024, 5:07am UTC](https://discourse.julialang.org/t/can-you-twiddle-bits-in-a-float64-variable/121271/2 "2024-10-14T05:07:25Z")

</div>

Why don’t you try?

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [October 14, 2024, 5:20am UTC](https://discourse.julialang.org/t/can-you-twiddle-bits-in-a-float64-variable/121271/3 "2024-10-14T05:20:52Z")

</div>

The `reinterpret` function could be worth a look 🙂

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 14, 2024, 9:51am UTC](https://discourse.julialang.org/t/can-you-twiddle-bits-in-a-float64-variable/121271/4 "2024-10-14T09:51:33Z")

</div>

[ldexp](https://github.com/JuliaLang/julia/blob/master/base/math.jl#L890) does this.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [October 14, 2024, 11:35am UTC](https://discourse.julialang.org/t/can-you-twiddle-bits-in-a-float64-variable/121271/5 "2024-10-14T11:35:57Z")

</div>

```julia
julia> xu = reinterpret(Unsigned, 1.2)
0x3ff3333333333333

julia> typeof(xu)
UInt64

julia> xu = xu + 1
0x3ff3333333333334

julia> xf = reinterpret(Float64, xu)
1.2000000000000002

julia> 

```

Thanks, this seems to work.

```julia
julia> xu = reinterpret(UInt64, 1.2)
0x3ff3333333333333

julia> typeof(xu)
UInt64

```

This seems to work as well, I wonder why it is called Unsigned instead of UInt64?

There also all thses strange masks I can muck around with

```julia
using .Base: sign_mask, exponent_mask, exponent_one,
            exponent_half, uinttype, significand_mask,
            significand_bits, exponent_bits, exponent_bias,
            exponent_max, exponent_raw_max, clamp, clamp!

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 14, 2024, 11:55am UTC](https://discourse.julialang.org/t/can-you-twiddle-bits-in-a-float64-variable/121271/6 "2024-10-14T11:55:46Z")

</div>

> [@StevenSiew](#):
>
> This seems to work as well, I wonder why it is called Unsigned instead of UInt64?

`Unsigned` is the supertype of `UInt64`:

```julia
julia> supertype(UInt64), supertype(UInt32), supertype(UInt16)
(Unsigned, Unsigned, Unsigned)

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [October 14, 2024, 2:45pm UTC](https://discourse.julialang.org/t/can-you-twiddle-bits-in-a-float64-variable/121271/7 "2024-10-14T14:45:49Z")

</div>

The fact that `Unsigned` (or `Signed`) works here due to some special casing (see `methods(reinterpret)`). It’s useful if you want to cast different width floats (`Float16`, `Float32`, `Float64`, or collectively `Base.IEEEFloat`) to `Base.BitInteger` with the same code, so that your bit-twiddling can work on multiple widths.

A related set of internal functions (i.e., they may not continue to work indefinitely) are:

```julia-repl
julia> Base.uinttype(Float64), Base.inttype(Float32), Base.floattype(UInt16)
(UInt64, Int32, Float16)

```
