# Would anyone please help me with this Boolean related error?

**URL:** <https://discourse.julialang.org/t/would-anyone-please-help-me-with-this-boolean-related-error/69856>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 15, 2021, 9:31pm UTC](https://discourse.julialang.org/t/would-anyone-please-help-me-with-this-boolean-related-error/69856 "2021-10-15T21:31:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 15, 2021, 9:31pm UTC](https://discourse.julialang.org/t/would-anyone-please-help-me-with-this-boolean-related-error/69856/1 "2021-10-15T21:31:45Z")

</div>

Anyone knows why I keep getting an error for the below simple function:

```julia
A = [15, 16, -30];
B = [12, 35, 38];
Ind = A .> -20 && B .>= 15

```

Here is the error:

```julia
TypeError: non-boolean (BitVector) used in boolean context

Stacktrace:
 [1] top-level scope
   @ In[12]:3
 [2] eval
   @ ./boot.jl:360 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1116

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 15, 2021, 9:35pm UTC](https://discourse.julialang.org/t/would-anyone-please-help-me-with-this-boolean-related-error/69856/2 "2021-10-15T21:35:29Z")

</div>

You didn’t broadcast the `&&`. Importantly, you _can’t_ broadcast the `&&` until 1.7 (which is in beta stage). It’s trying to do a boolean comparison with the vector result of `A .> -20`. But it can only compare booleans.

You want

```julia
map(A, B) do a, b
    (a > -20) && (b >= 15)
end 

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 15, 2021, 9:45pm UTC](https://discourse.julialang.org/t/would-anyone-please-help-me-with-this-boolean-related-error/69856/3 "2021-10-15T21:45:03Z")

</div>

Many thanks! As a Matlab user, I find that very inconvenient 🙂

What does map do? I have to convert my A to a?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 15, 2021, 9:55pm UTC](https://discourse.julialang.org/t/would-anyone-please-help-me-with-this-boolean-related-error/69856/4 "2021-10-15T21:55:41Z")

</div>

You can also do

```julia
Ind = A .> -20 .& B .>= 15

```

note, one `&` instead of two `&&`.

This could lead to some errors, though, since it won’t error on numbers. Its a bit-wise operation.

```julia
julia> 5 & 6
4

```

`map` applies a function to each element of an iterator, collecting the result into a `Vector`. It’s a fundamental idiom in Julia, see `? map` for more details.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [October 15, 2021, 10:00pm UTC](https://discourse.julialang.org/t/would-anyone-please-help-me-with-this-boolean-related-error/69856/5 "2021-10-15T22:00:34Z")

</div>

You may also do:

```julia
A = [15, 30, 45];
B = [20, 25, 30];
[a > 16 && b >= 30 for (a, b) in zip(A,B)]

```

which results in `[0, 0, 1]`.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [October 16, 2021, 3:20am UTC](https://discourse.julialang.org/t/would-anyone-please-help-me-with-this-boolean-related-error/69856/6 "2021-10-16T03:20:14Z")

</div>

> [@pdeffebach](#):
>
> You can also do
> 
> ```julia
> Ind = A .> -20 .& B .>= 15
> 
> ```
> 
> note, one `&` instead of two `&&` .

Unfortunately this is really dangerous, because the operator precedence of `&` is different from the precedence of `&&`:

```julia
julia> A = [15, 16, -30];

julia> B = [12, 35, 38];

julia> A .> -20 .& B .>= 15
3-element BitVector:
 0
 0
 0

julia> (A .> -20) .& (B .>= 15)
3-element BitVector:
 0
 1
 0

```

So you need parentheses to get the right behavior.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [October 16, 2021, 3:25am UTC](https://discourse.julialang.org/t/would-anyone-please-help-me-with-this-boolean-related-error/69856/7 "2021-10-16T03:25:52Z")

</div>

On a sufficiently new Julia, meaning 1.7 I think, you can broadcast `&&` too:

```julia
julia> A .> -20 .&& B .>= 15
3-element BitVector:
 0
 1
 0

```
