# Most efficient way of converting binary data to `AbstractVector{Bool}`

Suppose I have a `Vector{<:Unsigned}` and I want to convert the bits to some sort of `AbstractVector{Bool}`. I want to know the absolute most efficient way of doing that (I don’t even really care what kind of object I convert to, I just need to be able to check the booleans). Right now I just check one bit at a time (each check is very efficient, but the overall process is terrible).

I am aware of `BitArray`, but the constructor

``````BitArray{1}(data, length, dims)
``````

does not seem to exist and am not entirely sure if doing that would be the best solution anyway.

What bit operations do you wish to perform?
How long are your vectors, and is your code only going to run on 64-bit machines?

All I want to be able to do is have conditional statements that depend on whether the bits are `1`. I don’t care what bit operations that involves. Just to make this a little more vivid, see this similar example

``````A = rand(UInt8, 10)  # this object has 80 total bits

B = rand(Float64, 80)

for i ∈ 1:80
getbit(A, i) && (B[i] = NaN)
end

``````

It’s ok if instead `getbit(A)` returns an `AbstractVector{Bool}` of length 80, I don’t really care about any of the details, I just want the ability to check the bits as efficiently as possible.

Ideally I’d like the vectors to be any size, they may be small, but it’s most important that I can do this efficiently for vectors of sizes on the order of 10^8.

You’re right — `BitArray` doesn’t have any public or officially supported ways of doing this. If you have a `Vector{Uint64}`, then you can “sneak” the data in behind its back:

``````julia> A = rand(UInt64, 20)
B = BitArray(0)
B.chunks = A
B.len = length(A)*64
B
1280-element BitArray{1}:
false
true
true
false
⋮
``````

As always, beware that this may change/break at any point.

1 Like

It actually hadn’t even occurred to me that `BitArray` is mutable, but I guess it has to be because two of its fields have to change if it gets extended.

Thanks, I’ll check out how efficient this is.

It would of course be really nice if some more of the constructors of `BitArray` were exposed so this could be done more naturally.

3 Likes