Concatenating bit strings

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.

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

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

3 Likes

Thanks!!! A followup question: 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.)

2 Likes

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

UInt16(UInt(1) << 8 | UInt(2))
1 Like