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

**URL:** <https://discourse.julialang.org/t/most-efficient-way-of-converting-binary-data-to-abstractvector-bool/9261>\
**Category:** General Usage\
**Created:** [February 22, 2018, 6:42pm UTC](https://discourse.julialang.org/t/most-efficient-way-of-converting-binary-data-to-abstractvector-bool/9261 "2018-02-22T18:42:43Z")\
**Posts on this page:** 5\
**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:** [February 22, 2018, 6:42pm UTC](https://discourse.julialang.org/t/most-efficient-way-of-converting-binary-data-to-abstractvector-bool/9261/1 "2018-02-22T18:42:43Z")

</div>

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

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

```

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

---

<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:** [February 22, 2018, 11:37pm UTC](https://discourse.julialang.org/t/most-efficient-way-of-converting-binary-data-to-abstractvector-bool/9261/2 "2018-02-22T23:37:00Z")

</div>

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?

---

<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:** [February 23, 2018, 2:21pm UTC](https://discourse.julialang.org/t/most-efficient-way-of-converting-binary-data-to-abstractvector-bool/9261/3 "2018-02-23T14:21:58Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 23, 2018, 4:03pm UTC](https://discourse.julialang.org/t/most-efficient-way-of-converting-binary-data-to-abstractvector-bool/9261/4 "2018-02-23T16:03:30Z")

</div>

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
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.

---

<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:** [February 23, 2018, 4:06pm UTC](https://discourse.julialang.org/t/most-efficient-way-of-converting-binary-data-to-abstractvector-bool/9261/5 "2018-02-23T16:06:54Z")

</div>

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.
