# How to convert bytes to \`BitVector\`?

**URL:** https://discourse.julialang.org/t/how-to-convert-bytes-to-bitvector/112440
**Category:** General Usage
**Tags:** question
**Created:** [April 2, 2024, 8:15pm UTC](https://discourse.julialang.org/t/how-to-convert-bytes-to-bitvector/112440 "2024-04-02T20:15:53Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [April 2, 2024, 8:15pm UTC](https://discourse.julialang.org/t/how-to-convert-bytes-to-bitvector/112440/1 "2024-04-02T20:15:53Z")

</div>

I’m handed some bytes by a file format, and each bit represent a Bool – now, I know a little bit about how our BitVector works, so I have some snippet that’s quite fragile:

```julia
    total_num_elements = ...
    # pad to nearest 8*k bytes because each chunk needs to be UInt64
    bytes = ...
    append!(bytes, zeros(eltype(bytes), 8 - rem(total_num_elements, 8)))
    chunks = reinterpret(UInt64, bytes)

    res = BitVector(undef, total_num_elements)
    copyto!(res.chunks, chunks) # don't want jam ReinterpretArray into BitVector

```

but this fails for:

```julia
total_num_elements = 10000
bytes = rand(UInt8, 1250)

```

I can probably do a if-else to gate the logic so if we happen to land on divisible input, we don’t pad it. But in general I’m wondering if there’s a better way

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [April 2, 2024, 8:21pm UTC](https://discourse.julialang.org/t/how-to-convert-bytes-to-bitvector/112440/2 "2024-04-02T20:21:13Z")

</div>

> [@jling](#):
>
> `8 - rem(total_num_elements, 8))`

actually, maybe the fix is that this should be

```julia
(64 - rem(total_num_elements, 64)) \div 8

```
