# Question about precedence of operator ~ (bitwise not)

**URL:** https://discourse.julialang.org/t/question-about-precedence-of-operator-bitwise-not/133760
**Category:** General Usage
**Tags:** question
**Created:** [November 9, 2025, 3:08pm UTC](https://discourse.julialang.org/t/question-about-precedence-of-operator-bitwise-not/133760 "2025-11-09T15:08:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![CiccioPasticcio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cicciopasticcio/32/215909_2.png) [@CiccioPasticcio](https://discourse.julialang.org/u/CiccioPasticcio)
#### Post date: [November 9, 2025, 3:08pm UTC](https://discourse.julialang.org/t/question-about-precedence-of-operator-bitwise-not/133760/1 "2025-11-09T15:08:20Z")

</div>

I see that the precedence of operation is specified [here](https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm) and if i understand correctly the operations higer in the file have higer precedence, so ~ being at the top has higest precedence. Also using `Base.operator_precedence` I see that

```julia-auto
julia> Base.operator_precedence(:~)
1
julia> Base.operator_precedence(:+)
11
julia> Base.operator_precedence(:*)
12
julia> Base.operator_precedence(:^)
15

```

So this behaves as expected:

```julia-auto
julia> :(~3+2)
:(~3 + 2)
julia> :(~3*2)
:(~3 * 2)

```

Because ~ has higer precedence and its first applied on three, and then the + is applied  
but why does it behave like this then?

```julia-auto
julia> :(~3^2)
:(~(3 ^ 2))

```

I would expect `:((~x) ^ y)` as result

---

<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 9, 2025, 3:31pm UTC](https://discourse.julialang.org/t/question-about-precedence-of-operator-bitwise-not/133760/2 "2025-11-09T15:31:22Z")

</div>

`operator_precedence` is about **binary** operators, and larger numbers have higher precedence.

So, when it is treated as a binary operator, `~` has the _lowest_ precedence, the same as assignment `=`:

```julia-auto
julia> Base.operator_precedence(:(=))
1

julia> Base.operator_precedence(:(~))
1

```

And indeed, you can define `~` as a binary function:

```julia-auto
julia> :(a ~ b)
:(a ~ b)

julia> Base.:(~)(a, b) = a + b

julia> 3 ~ 4
7

```

The reason that it has assignment precedence was mainly intended for domain-specific languages via metaprogramming/macros, since `~` has an “assignment-like” meaning in statistics. In fact, `~` was originally a special “metaprogramming-only” binary operator that expanded into a macro call: [Make tilde automatically quote its arguments · Issue #4882 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/4882)

**Unary** operator precedence, on the other hand, is handled differently, and unary operators are higher precedence than `*` but are trickier for `^`. See e.g.

> [@Confused about operator precedence for 2^3x](https://discourse.julialang.org/t/confused-about-operator-precedence-for-2-3x/8214/7):
>
> [Unary] operators, including juxtaposition, bind tighter than `^` on the right but looser on the left.

… this is something that could be documented better. ([clarify Base.operator\_precedence docs: this is only for binary operators by stevengj · Pull Request #60087 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/60087) … other PRs could improve other portions of the documentation of this kind of thing.)

---

<div class="post-metadata">

### Author: ![CiccioPasticcio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cicciopasticcio/32/215909_2.png) [@CiccioPasticcio](https://discourse.julialang.org/u/CiccioPasticcio)
#### Post date: [November 12, 2025, 4:49pm UTC](https://discourse.julialang.org/t/question-about-precedence-of-operator-bitwise-not/133760/3 "2025-11-12T16:49:30Z")

</div>

> [@stevengj](#):
>
> **Unary** operator precedence, on the other hand, is handled differently, and unary operators are higher precedence than `*` but are trickier for `^`. See e.g.

thanks i understand now, but where is documented the precedence of unary operators then?

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [November 12, 2025, 8:28pm UTC](https://discourse.julialang.org/t/question-about-precedence-of-operator-bitwise-not/133760/4 "2025-11-12T20:28:52Z")

</div>

Some of it is described in [Mathematical Operations and Elementary Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity)
