# Newbie question - convert two 8-byte values into a single 16-byte value

**URL:** https://discourse.julialang.org/t/newbie-question-convert-two-8-byte-values-into-a-single-16-byte-value/7662
**Category:** New to Julia
**Created:** [December 9, 2017, 8:20pm UTC](https://discourse.julialang.org/t/newbie-question-convert-two-8-byte-values-into-a-single-16-byte-value/7662 "2017-12-09T20:20:09Z")
**Posts on this page:** 7
**Page:** 1

<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: [December 9, 2017, 8:20pm UTC](https://discourse.julialang.org/t/newbie-question-convert-two-8-byte-values-into-a-single-16-byte-value/7662/1 "2017-12-09T20:20:09Z")

</div>

Let’s say I have an array of 2 elements of UInt8.  
I need to make it into a single element of UInt16.  
How?

I thought about doing bit arithmetic (shifting/or’ing) but there’s got to be an easier way?

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [December 9, 2017, 8:32pm UTC](https://discourse.julialang.org/t/newbie-question-convert-two-8-byte-values-into-a-single-16-byte-value/7662/2 "2017-12-09T20:32:34Z")

</div>

You `reinterpret` e.g.

`reinterpret(UInt16, UInt8[1,2])`

---

<div class="post-metadata">

### Author: ![kkretschmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kkretschmer/32/7474_2.png) [@kkretschmer](https://discourse.julialang.org/u/kkretschmer)
#### Post date: [December 10, 2017, 4:19pm UTC](https://discourse.julialang.org/t/newbie-question-convert-two-8-byte-values-into-a-single-16-byte-value/7662/3 "2017-12-10T16:19:47Z")

</div>

And if your source has a fixed endianness that might be different from the host byte order of your machine, you can use `ntoh`, `hton`, `ltoh`, and `htol` from `base/io.jl` to convert. E.g. i use this to read a 16-bit big-endian scalar value from the UInt8 array `x`:

```julia
ntoh(reinterpret(UInt16, x[1:2])[1])

```

---

<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: [December 10, 2017, 4:53pm UTC](https://discourse.julialang.org/t/newbie-question-convert-two-8-byte-values-into-a-single-16-byte-value/7662/4 "2017-12-10T16:53:44Z")

</div>

Using arrays to do a little bit of bit manipulation is going to be very inefficient! This is not very hard to write with bit operations:

```julia
julia> bitcat(a::UInt8, b::UInt8) = (UInt16(a) << 8) | b
bitcat (generic function with 1 method)

julia> bitcat(0x12, 0x34)
0x1234

julia> @code_native bitcat(0x12, 0x34)
	.section	__TEXT,__ text,regular,pure_instructions
; Function bitcat {
; Location: REPL[23]:1
	shll	$8, %edi
	movzbl	%sil, %eax
	orl	%edi, %eax
	retq
	nopw	(%rax,%rax)
;}

```

---

<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: [December 10, 2017, 5:20pm UTC](https://discourse.julialang.org/t/newbie-question-convert-two-8-byte-values-into-a-single-16-byte-value/7662/5 "2017-12-10T17:20:57Z")

</div>

Stefan’s remark proven 😁

```julia
julia> bitcat(a::UInt8, b::UInt8) = (UInt16(a) << 8) | b
bitcat (generic function with 1 method)

julia> bitcat(x::Vector{UInt8}) = (UInt16(x[1]) << 8) | x[2]
bitcat (generic function with 2 methods)

```

Comparing `reinterpret`, `bitcat` taking individual arguments, and `bitcat` taking an array:

```julia
julia> @btime reinterpret(UInt16, [0x12, 0x34])
  78.048 ns (3 allocations: 176 bytes)
1-element Array{UInt16,1}:
 0x3412

julia> @btime reinterpret(UInt16, [0x12, 0x34])
  78.280 ns (3 allocations: 176 bytes)
1-element Array{UInt16,1}:
 0x3412

julia> @btime reinterpret(UInt16, [0x12, 0x34])
  77.307 ns (3 allocations: 176 bytes)
1-element Array{UInt16,1}:
 0x3412

julia> @btime bitcat([0x12, 0x34])
  39.327 ns (1 allocation: 96 bytes)
0x1234

julia> @btime bitcat([0x12, 0x34])
  39.276 ns (1 allocation: 96 bytes)
0x1234

julia> @btime bitcat([0x12, 0x34])
  39.401 ns (1 allocation: 96 bytes)
0x1234

julia> @btime bitcat(0x12, 0x34)
  1.760 ns (0 allocations: 0 bytes)
0x1234

julia> @btime bitcat(0x12, 0x34)
  1.759 ns (0 allocations: 0 bytes)
0x1234

julia> @btime bitcat(0x12, 0x34)
  1.760 ns (0 allocations: 0 bytes)
0x1234

```

---

<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: [December 10, 2017, 5:56pm UTC](https://discourse.julialang.org/t/newbie-question-convert-two-8-byte-values-into-a-single-16-byte-value/7662/6 "2017-12-10T17:56:47Z")

</div>

Actually I’m surprised that the array version is _only_ 20x slower. If you’re using `@btime` for timing I think you can just do it once since the whole purpose of that macro is to run something enough times for you automatically – and note that it seems to be working since the measurements of the same operation are all similar.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [December 11, 2017, 5:17am UTC](https://discourse.julialang.org/t/newbie-question-convert-two-8-byte-values-into-a-single-16-byte-value/7662/7 "2017-12-11T05:17:21Z")

</div>

Do be careful with

```julia
@btime bitcat([0x12, 0x34])

```

Note that

```julia
julia> @btime bitcat($([0x12, 0x34]))
  1.791 ns (0 allocations: 0 bytes)
0x1234

```

and

```julia
julia> @btime bitcat(x) setup = x = rand(UInt8, 2)
  1.737 ns (0 allocations: 0 bytes)
0xd87b

```

See [BenchmarkTools docs](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions).
