# \`Vector{Bool}\` to Int

**URL:** https://discourse.julialang.org/t/vector-bool-to-int/89560
**Category:** General Usage
**Tags:** question
**Created:** [October 31, 2022, 1:21pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560 "2022-10-31T13:21:05Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)
#### Post date: [October 31, 2022, 1:21pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/1 "2022-10-31T13:21:05Z")

</div>

I’m using PyCall to interact with a python package which returns integers as a `Vector{Bool}`, e.g. `[true, true, false, true, true, true, false, true]`. How can I easily convert this to an integer?

In a previous question it’s shown how to convert from a bool Str to Int

> [@Converting bitstring to number](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/2):
>
> Using v0.7 / master, it’s simple: parse(Int, "110101"; base=2)

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [October 31, 2022, 1:22pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/2 "2022-10-31T13:22:43Z")

</div>

Should be as simple as this:

```julia
julia> v = [true, true, false, true, true, true, false, true]
8-element Vector{Bool}:
 1
 1
 0
 1
 1
 1
 0
 1

julia> Int.(v)
8-element Vector{Int64}:
 1
 1
 0
 1
 1
 1
 0
 1

```

---

<div class="post-metadata">

### Author: ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)
#### Post date: [October 31, 2022, 1:24pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/3 "2022-10-31T13:24:50Z")

</div>

Sorry perhaps it wasn’t clear. I’d like to interpret it as

```nohighlight
parse(Int, "11011101"; base=2)

```

So the initial example gives `221`

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [October 31, 2022, 1:27pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/4 "2022-10-31T13:27:33Z")

</div>

It’s a bit esoteric, but this works:

```julia
julia> only(BitVector(reverse(v)).chunks) |> Int
221

```

There may be a better way.

---

<div class="post-metadata">

### Author: ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)
#### Post date: [October 31, 2022, 1:32pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/5 "2022-10-31T13:32:47Z")

</div>

Slightly esoteric but it does work, unless someone has a clearer solution I’ll stick with this. Thank you.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [October 31, 2022, 1:47pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/6 "2022-10-31T13:47:24Z")

</div>

You can just use the `String` method in your OP if you construct the string:

```julia
julia> parse(Int, join(Int.(v), ""), base = 2)
221

```

maybe less esoteric, but probably less efficient than the current solution.

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [October 31, 2022, 1:59pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/7 "2022-10-31T13:59:51Z")

</div>

Less esoteric but still fast:

```julia
julia> evalpoly(2, reverse(v))
221

```

More esoteric and faster yet:

```julia
julia> mapreduce(((i, v),) -> (v << (i - 1)), |, enumerate(Iterators.reverse(v)))
221

```

---

<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: [October 31, 2022, 2:01pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/8 "2022-10-31T14:01:22Z")

</div>

I think the `evalpoly` is the cleanest and not so slow

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [October 31, 2022, 7:17pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/9 "2022-10-31T19:17:09Z")

</div>

By modifying the base function so that you don’t have to reverse v, you gain performance

```julia

function _evalpoly(x, p)
    N = length(p)
    ex = p[1]
    for i in 2:N
        ex *= x
        ex+=p[i]
    end
    ex
end

```

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [November 1, 2022, 12:37am UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/10 "2022-11-01T00:37:46Z")

</div>

In my opinion the cleanest, and 4x faster than the fastest `mapreduce` solution above,:

```julia
reduce((acc, b) -> acc << 1 + b, v; init=0)

```

Its just 8 bitshifts and 8 additions and is pretty self explanatory, we just shift the `Int` accumulator left and add the next `Bool` as the lowest bit

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [November 1, 2022, 4:48pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/11 "2022-11-01T16:48:28Z")

</div>

really nice, but not the fastest

```julia
julia> using BenchmarkTools

julia> function polyeval(x, p)
           N = length(p)
           ex = p[1]
           for i in 2:N
               ex *= x
               ex+=p[i]
           end
           ex
       end
polyeval (generic function with 1 method)

julia> @btime polyeval(2,v)
  16.533 ns (0 allocations: 0 bytes)
221

julia> @btime reduce((x,y)->x<<1+y, v; init=0)
  70.902 ns (0 allocations: 0 bytes)
221

julia> @btime evalpoly(2, reverse(v))
  55.589 ns (1 allocation: 64 bytes)
221

julia> @btime mapreduce(((i, v),) -> (v << (i - 1)), |, enumerate(Iterators.reverse(v)))
  150.181 ns (3 allocations: 48 bytes)
221

julia> @btime only(BitVector(reverse(v)).chunks) |> Int
  316.949 ns (3 allocations: 160 bytes)
221

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [November 1, 2022, 5:01pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/12 "2022-11-01T17:01:40Z")

</div>

The base for loop performs even better if it specializes in the specific case of base 2 numbers

```julia
julia> function poly_2_eval(p)
           ex = 0
           for e in p
               ex *= 2
               ex+=e
           end
           ex
       end
poly_2_eval (generic function with 1 method)

julia> @btime poly_2_eval(v)
  15.030 ns (0 allocations: 0 bytes)
221

```

---

<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: [November 1, 2022, 5:04pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/13 "2022-11-01T17:04:31Z")

</div>

> [@rocco\_sprmnt21](#):
>
> The base for loop performs even better if it specializes in the specific case of base 2 numbers

It’s fun to play the benchmarks game, but let’s step back here and remember the context — this `Vector{Bool}` is coming _from a Python function_. Performance of a linear-time conversion is unlikely to matter in this context, so I would just go with any solution that is clear and requires little code, e.g. `evalpoly(2, reverse(v))`.

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [November 1, 2022, 5:38pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/14 "2022-11-01T17:38:16Z")

</div>

@rocco_sprmnt21 Its not the fastest because you are not using `$` in your benchmarks, so most of the time is in how the type instability is handled, in `polyeval` it just quickly hits a function barrier.

Here with `$` interpolation:

```julia
julia> @btime polyeval(2,$v)
  7.856 ns (0 allocations: 0 bytes)
221

julia> @btime reduce((x,y)->x<<1+y, $v; init=0)
  3.825 ns (0 allocations: 0 bytes)
221

julia> @btime evalpoly(2, reverse($v))
  43.112 ns (1 allocation: 64 bytes)
221

julia> @btime mapreduce(((i, v),) -> (v << (i - 1)), |, enumerate(Iterators.reverse($v)))
  13.070 ns (0 allocations: 0 bytes)
221

julia> @btime only(BitVector(reverse($v)).chunks) |> Int
  63.108 ns (3 allocations: 160 bytes)
221

```

@stevengj I mostly posted my solution for the simplicity. `evalpoly` is less clear to me than `<<` because I have to think more about about what `evalpoly` is doing behind the scenes - it’s not a function I use much, where as `reduce` and `<<` are generic. Of course that will vary person to person.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [November 1, 2022, 9:07pm UTC](https://discourse.julialang.org/t/vector-bool-to-int/89560/15 "2022-11-01T21:07:51Z")

</div>

The loop I proposed is exactly the one (with a few modifications) with which the evalpoly function is built.  
I did not know of this function (and I think it is not very well known in general).  
This is why I believe that @Raf’s solution with reduce is preferable.  
On the other hand, I believe that the two functions reduce (…) and poly\_2\_eval are, in a sense, isomorphic.  
They probably have the same lowered code.  
In fact, in terms of performance they are practically the same …

```julia
julia> v = [true, true, false, true, true, true, false, true]
8-element Vector{Bool}:
 1
 1
 0
 1
 1
 1
 0
 1

julia> function poly_2_eval(p)
           ex = 0
           for e in p
               ex *= 2
               ex+=e
           end
           ex
       end
poly_2_eval (generic function with 1 method)

julia> @btime reduce((x,y)->x<<1+y, $v; init=0)
  4.600 ns (0 allocations: 0 bytes)
221

julia> @btime reduce((x,y)->x*2+y, $v; init=0)
  4.600 ns (0 allocations: 0 bytes)
221

julia> @btime poly_2_eval($v)
  4.200 ns (0 allocations: 0 bytes)
221

```
