# \`reinterpret\` to a single value from an array of a smaller data type

**URL:** https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932
**Category:** General Usage
**Created:** [March 23, 2018, 11:56pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932 "2018-03-23T23:56:38Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [March 23, 2018, 11:56pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/1 "2018-03-23T23:56:38Z")

</div>

Currently on 0.7 if I want to `reinterpret` from a smaller data type to a larger one, I have to return an array, e.g.

```julia
reinterpret(Float64, zeros(UInt8, 8))
# returns Float64[0.0]

```

This results in an unnecessary allocation and interpret being unbelievably expensive for retrieving individual elements (by a factor of 3, according to my experiments). Is there any “safe” way around this, i.e. a safe equivalent to `unsafe_load`, even in principle? This seems really important if `reinterpret` has any chance of being a viable alternative to `unsafe_` methods.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [March 24, 2018, 1:31am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/2 "2018-03-24T01:31:09Z")

</div>

Can you pre-allocate the reinterpreted array and then just write into its source data? For example:

```julia
julia> x = zeros(UInt8, 8)
8-element Array{UInt8,1}:
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00

julia> y = reinterpret(Float64, x)
1-element reinterpret(Float64, ::Array{UInt8,1}):
 0.0

julia> x[1] = 1
1

julia> y
1-element reinterpret(Float64, ::Array{UInt8,1}):
 5.0e-324

```

Now you can copy into `x` as many times as you want and then copy out the result by taking `y[1]`. Still potentially expensive, but at least you don’t have to keep allocating new arrays.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [March 24, 2018, 1:46am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/3 "2018-03-24T01:46:48Z")

</div>

That doesn’t really work, it would require the larger type array (in this case `Float64`) to be persistent, which seems impractical.

Basically the use case is that I have a big `Vector{UInt8}` and I want to be able to pull values from it without allocating.

As I was writing this I suddenly realized that the intended use of `reinterpret` is probably to have a `ReinterpretArray` just sitting around. This is so different from how I was doing things with `unsafe_wrap` that now I’m kind of terrified…

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [March 24, 2018, 2:32am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/4 "2018-03-24T02:32:09Z")

</div>

Did you see

```julia
>?reinterpret

```

Warning: It is not allowed to reinterpret to an array to an element type with a larger alignment than the alignment of the array. For example, reinterpret(UInt32, UInt8[0, 0, 0, 0]) is not allowed

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [March 24, 2018, 2:33am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/5 "2018-03-24T02:33:57Z")

</div>

That’s on 0.6, we are on 0.7.

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [March 24, 2018, 4:59am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/6 "2018-03-24T04:59:17Z")

</div>

There was also a thread about converting bits to ints, where a way to [wrap a type](https://discourse.julialang.org/t/covert-bitarray-to-int64/9193/5) for bit access came up that may be handy. There doesn’t appear to be binary `&` methods defined for float types in 0.6 though

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 24, 2018, 6:40am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/7 "2018-03-24T06:40:37Z")

</div>

Use bit shifts. Shift each Uint8 with a different amount and or them together.

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [March 24, 2018, 7:20am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/8 "2018-03-24T07:20:46Z")

</div>

@ExpandingMan Take a look at SASLib.jl’s [conversion function](https://github.com/tk3369/SASLib.jl/blob/e1e39f07a9ef09bbca35cab7557dc3bbca7aa57b/src/utils.jl#L166)  
It should work in both Julia v0.6 and v0.7.

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [March 24, 2018, 8:53am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/9 "2018-03-24T08:53:09Z")

</div>

A bit off-topic, but are floating-point types in julia kind of opaque?, as reinterpret falls back to a ccall, and other binary methods like `&, |, <<` haven’t methods for floats.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 24, 2018, 9:06am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/10 "2018-03-24T09:06:34Z")

</div>

What do you suggest eg `&` do with a `Float64`?

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [March 24, 2018, 9:19am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/11 "2018-03-24T09:19:44Z")

</div>

Float64 is still a bitstype, so it could be that `0.0 & 0x01` would give a first (or last) bit value, not minding the length difference for that example. It’s not too unreasonable that they aren’t defined though

It might be easier to define a native julia floating or fixed point value

This works too

```julia
x = IOBuffer();
write(x, Float64(1));
seek(x,0);
z = read(x)

```

---

<div class="post-metadata">

### Author: ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)
#### Post date: [March 24, 2018, 9:53am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/12 "2018-03-24T09:53:27Z")

</div>

If you have a tuple of `UInt8`’s, the following gets completely optimized out for all Unsigned types:

> [@Type piracy?](https://discourse.julialang.org/t/type-piracy/9713/7):
>
> I was just toying around with this and found the following weird performance issue (Julia Version 0.7.0-DEV.4570): function f(::Type{UInt16}, x::NTuple{2, UInt8}) x1 = 0x0001 \* x[1] x2 = 0x0100 \* x[2] x1 + x2 end const NBytes{N} = NTuple{N, UInt8} function f(::Type{UInt16}, x::NBytes{2}) x1 = 0x0001 \* x[1] x2 = 0x0100 \* x[2] x1 + x2 end julia\> @btime f(UInt16, (0x01, 0x02)) 0.017 ns (0 allocations: 0 bytes) 0x0201 julia\> x = NBytes((0x01, 0x02)) (0x01, 0x02) julia\> …

I haven’t found a no op way to create byte tuples from `UIntX`.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 24, 2018, 12:05pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/13 "2018-03-24T12:05:44Z")

</div>

> [@y4lu](#):
>
> It might be easier to define a native julia floating or fixed point value

AFAICT the “standard” technique for that is with `reinterpret`, see eg

> **[GitHub - JuliaMath/FixedPointNumbers.jl: fixed point types for julia](https://github.com/JuliaMath/FixedPointNumbers.jl)**
>
> fixed point types for julia. Contribute to JuliaMath/FixedPointNumbers.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [March 24, 2018, 1:39pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/14 "2018-03-24T13:39:14Z")

</div>

Thanks for all the responses.

I think it would be really nice if we had this ability in `Base` with some variant of `reinterpret`. I’ve been working on an IO project that involves retrieving data from and writing data to IO buffers and I was hopeful that `reinterpret` would be able to replace all of the `unsafe` methods at some point in the near future, but I think we are still very far from that point. It’s definitely going to be really difficult to get it there, because for applications like this it’s going to be absolutely performance critical, and getting `reinterpret` performance to be on par with that of direct reference wrapping is going to be challenging.

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [March 24, 2018, 2:14pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/15 "2018-03-24T14:14:51Z")

</div>

The very basics are mostly working, if it can imitate a Float64 then it may give fairly fast/easy bit access

```julia
module xf8
  type fixedp8
    var1::UInt8;
    var2::UInt8;
    end;
  v1(x::Number) = floor(UInt8, x%256);
  v2(x::Number) = v1(256*(x-v1(x)));
  valFl(x::fixedp8) = Float64(x.var1) + x.var2/256;
  valUi(x::fixedp8) = UInt16(x.var1)*256 + x.var2;
  fixedp8(x::Number) = fixedp8(v1(x), v2(x));
  fixedp8(x::UInt16) = fixedp8(UInt8(floor(x/256)), x%256);
  Base.:+(x::fixedp8, y::fixedp8) = fixedp8( valUi(x)+valUi(y));
  Base.:-(x::fixedp8, y::fixedp8) = fixedp8( valUi(x)-valUi(y));
  Base.:*(x::fixedp8, y::fixedp8) = fixedp8( valUi(x)*valUi(y));
  Base.display(x::fixedp8) = print(valFl(x));
  end;
```

---

<div class="post-metadata">

### Author: ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)
#### Post date: [March 24, 2018, 2:25pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/16 "2018-03-24T14:25:02Z")

</div>

I would love to have a version or `reinterpret` that takes a bitstype and returns a `NTuple{N, UInt8}` without overhead.

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [March 24, 2018, 5:20pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/17 "2018-03-24T17:20:31Z")

</div>

> [@kristoffer.carlsson](#):
>
> Use bit shifts. Shift each Uint8 with a different amount and or them together.

That’s rather slow though.  
To get speed, you’d need to get a pointer, which you can reinterpret to a different pointer type  
(essentially a no-op) (although beware of alignment issues on the 32-bit ARM platform).

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [March 24, 2018, 5:25pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/18 "2018-03-24T17:25:37Z")

</div>

You might want to take a look at what I’m doing in the [Strs](https://github.com/JuliaString/Strs.jl) package.  
To have fast access to codeunits of various sizes (1, 2, 4 bytes), but keeping them stored in a buffer allocated as a `String` with `Base._string_n`, I have two functions `get_codeunit` and `set_codeunit!`.  
These are used to wrap up the `unsafe_load` and `unsafe_store!` operations.  
I also use the `GC.@preserve` macro to be safe, and make sure all bounds checking is done in advance.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [March 24, 2018, 6:10pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/19 "2018-03-24T18:10:30Z")

</div>

> [@ScottPJones](#):
>
> To get speed, you’d need to get a pointer, which you can reinterpret to a different pointer type (essentially a no-op)

Yeah, it does seem a bit crazy. Like I said above, I think we have a long way to go before we can do IO stuff without the use of `Ptr`. I believe we can get there, but I don’t think it’s going to be a 1.0 thing as I had initially hoped.

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [March 26, 2018, 4:29am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/20 "2018-03-26T04:29:12Z")

</div>

> [@ExpandingMan](#):
>
> retrieving data from and writing data to IO buffers

There’s many methods already provided for safely reading/writing primitive bit elements from IO. For example, `read(io, Float64)` or `read!(io, zeros(Float64, N))`. It’s also fairly easy to define a new one: `read(io, ::Type{T}) where {T <: S} = read!(io, Ref{T}())[]` and `write(io, x::S) = write(io, Ref(x))`

> [@y4lu](#):
>
> module xf8

Have you seen [FixedPointNumbers](https://github.com/JuliaMath/FixedPointNumbers.jl)?

[Next page](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932.md?page=2)
