# Difficulties with vectorized logical operations due to operator precedence

**URL:** <https://discourse.julialang.org/t/difficulties-with-vectorized-logical-operations-due-to-operator-precedence/39137>\
**Category:** General Usage\
**Created:** [May 8, 2020, 8:12pm UTC](https://discourse.julialang.org/t/difficulties-with-vectorized-logical-operations-due-to-operator-precedence/39137 "2020-05-08T20:12:41Z")\
**Posts on this page:** 8\
**Page:** 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:** [May 8, 2020, 8:12pm UTC](https://discourse.julialang.org/t/difficulties-with-vectorized-logical-operations-due-to-operator-precedence/39137/1 "2020-05-08T20:12:41Z")

</div>

I run into this problem all the time. Here’s an example where the vector `x` contains two kinds of missing, the `missing` singleton and a sentinel value of `99`.

```julia
julia> x = [1, 99, missing, 99, 2, missing, 1];

julia> xmissing1 = ismissing.(x) .| x .== 99
7-element BitArray{1}:
 0
 1
 0
 1
 0
 0
 0

julia> xmissing2 = ismissing.(x) .| (x .== 99)
7-element BitArray{1}:
 0
 1
 1
 1
 0
 1
 0

julia> xmissing1 == xmissing2
false

```

The problem here is that `|` has a [higher precedence](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity-1) than `==`, which I find unexpected. In my experience, it seems that in other languages comparisons have a higher precedence than logical conjunctions. For example, [here is the precedence table for R](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Syntax.html).

Of course, in Julia, comparisons do have a higher precedence than `&&` and `||`, but those are not broadcastable.

Does anyone else get bit by this behavior? Does the higher precedence of `|` and `&` have anything to do with making vectorized chained comparisons work?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 8, 2020, 8:28pm UTC](https://discourse.julialang.org/t/difficulties-with-vectorized-logical-operations-due-to-operator-precedence/39137/2 "2020-05-08T20:28:11Z")

</div>

> [@CameronBieganek](#):
>
> The problem here is that `|` has a [higher precedence](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity-1) than `==` , which I find unexpected.

When you write `a = 3 | 4` you expect `a` to be `3`? That’s at least not what I would expect.

---

<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:** [May 8, 2020, 8:31pm UTC](https://discourse.julialang.org/t/difficulties-with-vectorized-logical-operations-due-to-operator-precedence/39137/3 "2020-05-08T20:31:32Z")

</div>

Yes, frequently. I really want to add dot support to `.&&` and `.||` at some point, but it requires front-end lisp hackery that I’ve not had a chance to do.

Ref. [https://github.com/JuliaLang/julia/issues/5187#issuecomment-327905053](https://github.com/JuliaLang/julia/issues/5187#issuecomment-327905053)

---

<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:** [May 8, 2020, 8:43pm UTC](https://discourse.julialang.org/t/difficulties-with-vectorized-logical-operations-due-to-operator-precedence/39137/4 "2020-05-08T20:43:14Z")

</div>

> [@kristoffer.carlsson](#):
>
> When you write `a = 3 | 4` you expect `a` to be `3` ?

Um, not sure I follow you. My example used `==`, not `=`. Assignment does have lower precedence than comparison operators, as expected.

To clarify, I never use `|` and `&` as bitwise operators on numbers, I only use them for vectorized boolean operations. So I have no expectation regarding what `3 | 4` should equal.

---

<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:** [May 8, 2020, 8:45pm UTC](https://discourse.julialang.org/t/difficulties-with-vectorized-logical-operations-due-to-operator-precedence/39137/5 "2020-05-08T20:45:19Z")

</div>

> [@mbauman](#):
>
> I really want to add dot support to `.&&` and `.||`

Would the vectorized `&&` and `||` use three-value logic? Because of course right now the scalar versions do not:

```julia
julia> missing || true
ERROR: TypeError: non-boolean (Missing) used in boolean context
Stacktrace:
 [1] top-level scope at REPL[56]:1

```

---

<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:** [May 8, 2020, 8:52pm UTC](https://discourse.julialang.org/t/difficulties-with-vectorized-logical-operations-due-to-operator-precedence/39137/6 "2020-05-08T20:52:11Z")

</div>

No, I would want and expect them to behave exactly like their scalar counterparts.

You are right, however, that this would appear quite frequently with missing data.

---

<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:** [May 8, 2020, 8:57pm UTC](https://discourse.julialang.org/t/difficulties-with-vectorized-logical-operations-due-to-operator-precedence/39137/7 "2020-05-08T20:57:27Z")

</div>

Hmmm, well then I’m afraid I wouldn’t be able to use `.||` and `.&&` very often. E.g., in the above example, `x .== 99` contains `missing`s, so I would still have to use `.|`.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 9, 2020, 8:08am UTC](https://discourse.julialang.org/t/difficulties-with-vectorized-logical-operations-due-to-operator-precedence/39137/8 "2020-05-09T08:08:41Z")

</div>

> [@mbauman](#):
>
> add dot support to `.&&` and `.||` at some point

Until then, one can do something like

```julia
(x -> ismissing(x) || x == 99).(x)

```
