# Truth values, precedence and arithmetic operators

**URL:** <https://discourse.julialang.org/t/truth-values-precedence-and-arithmetic-operators/122200>\
**Category:** General Usage\
**Tags:** operator\
**Created:** [November 3, 2024, 3:17pm UTC](https://discourse.julialang.org/t/truth-values-precedence-and-arithmetic-operators/122200 "2024-11-03T15:17:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [November 3, 2024, 3:17pm UTC](https://discourse.julialang.org/t/truth-values-precedence-and-arithmetic-operators/122200/1 "2024-11-03T15:17:47Z")

</div>

While making a code, I had the following condition to throw an error:

```julia
n<0 | !isa(n,Integer)

```

For n=3.0, the code was giving false. Though it took me a while, I now understand that “|” takes precedence. Even though, in the docs, it says that Julia (unlike Python, but like Java) throws an error when trying to use integers in a local context, but when I do

```julia
n<0 | (!isa(n,Integer))

```

it aparently makes

```julia
n<(0|(true))

```

which gives

```julia
n<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:** [November 3, 2024, 3:32pm UTC](https://discourse.julialang.org/t/truth-values-precedence-and-arithmetic-operators/122200/2 "2024-11-03T15:32:38Z")

</div>

when in doubt, you can always check it:

```julia
julia> Meta.@lower n<0 | !isa(n,Integer)
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = <
│ %2 = n
│ %3 = |
│ %4 = !
│ %5 = n isa Integer
│ %6 = (%4)(%5)
│ %7 = (%3)(0, %6)
│ %8 = (%1)(%2, %7)
└── return %8
))))

```

so the `(%3)(0, %6)` is doing `|(0, !(n isa Integer)) first, as you have realized.

the second thing is that `|` is bitwise or, you probably wanted `||` for boolean or?

---

<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:** [November 3, 2024, 3:34pm UTC](https://discourse.julialang.org/t/truth-values-precedence-and-arithmetic-operators/122200/3 "2024-11-03T15:34:56Z")

</div>

actually if you use `||`, the lowering is different (duh

```julia
julia> Meta.@lower n<0 || !isa(n,Integer)
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = n < 0
└── goto #3 if not %1
2 ─ return %1
3 ─ %4 = !
│ %5 = n isa Integer
│ %6 = (%4)(%5)
└── return %6
))))

```

because:

```julia
julia> Base.operator_precedence(:|)
11

julia> Base.operator_precedence(:||)
5

```

---

<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 3, 2024, 7:40pm UTC](https://discourse.julialang.org/t/truth-values-precedence-and-arithmetic-operators/122200/4 "2024-11-03T19:40:15Z")

</div>

> [@martin\_sanchez](#):
>
> Though it took me a while, I now understand that “|” takes precedence.

The point is that `|` in Julia is bitwise OR, and is treated like an arithmetic operator similar to `+` (hence having higher precedence than `<`).

Usually, for logical OR in Julia, people use `||`, which has lower precedence than `<` and is probably what you want here.

(The caveat is that `||` is short-circuiting. Very occasionally, for performance-critical code in a tight loop, it is sometimes faster to use the non-short-circuiting `|`. But in this case you usually need explicit parentheses to make the precedence come out right.)
