# BitVector to Bool

**URL:** <https://discourse.julialang.org/t/bitvector-to-bool/72359>\
**Category:** New to Julia\
**Created:** [December 1, 2021, 10:54am UTC](https://discourse.julialang.org/t/bitvector-to-bool/72359 "2021-12-01T10:54:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)\
**Post date:** [December 1, 2021, 10:54am UTC](https://discourse.julialang.org/t/bitvector-to-bool/72359/1 "2021-12-01T10:54:19Z")

</div>

Hi everyone,

I have two arrays :

```julia
x = [2, 7]
a = [1,2,5,8,4,9]

```

I would like to know if x is in a so I do that :

> x .∈ Ref(a)

This returns :

```julia
2-element BitVector:
 1
 0

```

How can I get the same result but with true and false ([true, false] in this case) instead of 1 and 0 ? So I can use this as a condition :

> if x .∈ Ref(a)  
> do …  
> end

When I try it like that i get this error : non-boolean (BitVector) used in boolean context

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:** [December 1, 2021, 11:04am UTC](https://discourse.julialang.org/t/bitvector-to-bool/72359/2 "2021-12-01T11:04:51Z")

</div>

You already have:

```julia
julia> result = in(x)(a)
false

julia> result = x .∈ Ref(a)
2-element BitVector:
 1
 0

julia> result[1]
true

```

The `0` and `1` you see are just how `true`/`false` are printed in arrays for compactness.

---

<div class="post-metadata">

**Author:** ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)\
**Post date:** [December 1, 2021, 12:01pm UTC](https://discourse.julialang.org/t/bitvector-to-bool/72359/4 "2021-12-01T12:01:01Z")

</div>

Why do I have an error : “non-boolean (BitVector) used in boolean context” when I want to use it as a condition in an if statement ?

---

<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:** [December 1, 2021, 12:05pm UTC](https://discourse.julialang.org/t/bitvector-to-bool/72359/5 "2021-12-01T12:05:32Z")

</div>

Probably because you are doing something like this?

```julia
julia> if result
           2+2
       end
ERROR: TypeError: non-boolean (BitVector) used in boolean context

```

The error is clear if maybe a bit subtle - a `BitVector` is not a boolean, it is a **vector** of booleans, so you can’t use it as a boolean value in an if statement.

---

<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:** [December 1, 2021, 12:54pm UTC](https://discourse.julialang.org/t/bitvector-to-bool/72359/6 "2021-12-01T12:54:56Z")

</div>

> [@jnewbie](#):
>
> if x .∈ Ref(a)

Maybe you mean `if all(in(a), x)`, to check whether _all_ of the elements of `x` are in `a`, or `if any(in(a), x)` to check whether _any_ elements of `x` are in `a`?

---

<div class="post-metadata">

**Author:** ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)\
**Post date:** [December 1, 2021, 12:55pm UTC](https://discourse.julialang.org/t/bitvector-to-bool/72359/7 "2021-12-01T12:55:57Z")

</div>

Thanks this is what I was looking for !

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [December 2, 2021, 8:54pm UTC](https://discourse.julialang.org/t/bitvector-to-bool/72359/8 "2021-12-02T20:54:29Z")

</div>

> [@jnewbie](#):
>
> How can I get the same result but with true and false ([true, false] in this case) instead of 1 and 0 ?

```julia
julia> map(x -> x in a, x)
2-element Vector{Bool}:
 1
 0

```

or

```julia
julia> (x -> x in a).(x)
2-element BitVector:
 1
 0

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 2, 2021, 10:30pm UTC](https://discourse.julialang.org/t/bitvector-to-bool/72359/9 "2021-12-02T22:30:55Z")

</div>

I think @jnewbie is not aware that `Bool` is a `Number` type in Julia and many times it is displayed as `0`/`1` instead of `false`/`true`. Their first example already had `Bool`s I think.
