# Concatenating bit strings

**URL:** <https://discourse.julialang.org/t/concatenating-bit-strings/54950>\
**Category:** New to Julia\
**Tags:** strings\
**Created:** [February 9, 2021, 7:13pm UTC](https://discourse.julialang.org/t/concatenating-bit-strings/54950 "2021-02-09T19:13:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsuwal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bsuwal/32/19577_2.png) [@bsuwal](https://discourse.julialang.org/u/bsuwal)\
**Post date:** [February 9, 2021, 7:13pm UTC](https://discourse.julialang.org/t/concatenating-bit-strings/54950/1 "2021-02-09T19:13:55Z")

</div>

Say I have a bit string `x = UInt(1)` (which is `0x01`) and `y = UInt(2)` (which is `0x02`).

Is there a way to concatenate these bit strings using something like `x * y` so that the resulting bit string would represent 0x01 0x02 and occupy two bytes in memory?

I would like to simply concatenate these two bit strings and use the result as a key to a dictionary, but would rather not allocate an array for it because I do it many times.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [February 9, 2021, 7:19pm UTC](https://discourse.julialang.org/t/concatenating-bit-strings/54950/2 "2021-02-09T19:19:16Z")

</div>

```julia
julia> UInt128(0x01) << 64 | UInt128(0x02)
0x00000000000000010000000000000002

```

Of course, you could use smaller sized values if your largest expected bit strings fit.

---

<div class="post-metadata">

**Author:** ![bsuwal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bsuwal/32/19577_2.png) [@bsuwal](https://discourse.julialang.org/u/bsuwal)\
**Post date:** [February 9, 2021, 7:27pm UTC](https://discourse.julialang.org/t/concatenating-bit-strings/54950/3 "2021-02-09T19:27:41Z")

</div>

Thanks!!! A followup question: Is UInt128 the largest a bit string can be?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 9, 2021, 7:37pm UTC](https://discourse.julialang.org/t/concatenating-bit-strings/54950/4 "2021-02-09T19:37:43Z")

</div>

> [@bsuwal](#):
>
> Is UInt128 the largest a bit string can be?

You can use a `BitVector` if you want a larger type. (For larger strings, the CPU will have to process the bit string in chunks, but `BitVector` mostly hides this from you.)

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [February 11, 2021, 4:42pm UTC](https://discourse.julialang.org/t/concatenating-bit-strings/54950/5 "2021-02-11T16:42:18Z")

</div>

If you are happy with UInt128 then great, but that is 16 bytes and you wanted 2 bytes…which would be something like:

```julia
UInt16(UInt(1) << 8 | UInt(2))

```
