# Inverse of bitstring(x)

**URL:** https://discourse.julialang.org/t/inverse-of-bitstring-x/91032
**Category:** General Usage
**Tags:** bitstring
**Created:** [November 30, 2022, 6:09am UTC](https://discourse.julialang.org/t/inverse-of-bitstring-x/91032 "2022-11-30T06:09:58Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [November 30, 2022, 6:09am UTC](https://discourse.julialang.org/t/inverse-of-bitstring-x/91032/1 "2022-11-30T06:09:58Z")

</div>

What function `f` is `f(typeof(x), bitstring(x)) == x` ?

---

<div class="post-metadata">

### Author: ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)
#### Post date: [November 30, 2022, 6:32am UTC](https://discourse.julialang.org/t/inverse-of-bitstring-x/91032/2 "2022-11-30T06:32:14Z")

</div>

```julia
f(T, s) = reinterpret(T, parse(Int, s; base = 2))

```

?

EDIT: This is not general enough (see @rafael.guerra [post below](https://discourse.julialang.org/t/inverse-of-bitstring-x/91032/4)). The general answer adopted form @JeffreySarnoff’s solution would look like:

```julia
f(T, s) = reinterpret(T, Meta.parse("0b" * s))

```

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [November 30, 2022, 6:55am UTC](https://discourse.julialang.org/t/inverse-of-bitstring-x/91032/3 "2022-11-30T06:55:52Z")

</div>

Not quite, it seems

```julia
f(T, s) = reinterpret(T, parse(Int, s; base = 2))
julia> let x =rand(Float16)
       f(typeof(x), bitstring(x)) == x
       end
ERROR: bitcast: argument size does not match size of target type
Stacktrace:
   [1] reinterpret(#unused#::Type{Float16}, x::Int64)
     @ Base ./essentials.jl:438
   [2] f(T::Type, s::String)
     @ Main ./REPL[1]:1

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 30, 2022, 11:06am UTC](https://discourse.julialang.org/t/inverse-of-bitstring-x/91032/4 "2022-11-30T11:06:20Z")

</div>

Fyi, there is a solution in [this other post](https://discourse.julialang.org/t/how-do-i-convert-a-binary-string-to-a-floating-point-number/21380/3).

---

<div class="post-metadata">

### Author: ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)
#### Post date: [December 1, 2022, 4:25am UTC](https://discourse.julialang.org/t/inverse-of-bitstring-x/91032/5 "2022-12-01T04:25:29Z")

</div>

I intended to cover this with `BitsX.jl`, but found it was missing. I added a [method to do this](https://github.com/jlapeyre/BitsX.jl/blob/49b790913d655f277b8c97d69e1af23aa2d89492/src/parse.jl#L43-L45).

You can define the function like this:

```julia
test_identity(x) = parse_bin(typeof(x), bitstring(x))

```

Works for many types, including `FloatX`.

`parse_bin` is similar to `Base.parse(T, s; base=2)`. But it is much faster and also allows filtering out formatting characters:

```julia
julia> s = BitsX.randbitstring(64)
"1100101110101110011000111100111000101101111011101010111100101000"

julia> @btime parse(UInt, $s; base=2);
  197.869 ns (0 allocations: 0 bytes)

julia> @btime parse_bin(UInt, $s);
  53.767 ns (0 allocations: 0 bytes)

```
