# 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:** 1
**Showing post:** 2

<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.)

---

_[View the full topic](https://discourse.julialang.org/t/question-about-precedence-of-operator-bitwise-not/133760)._
