# What's the difference between the tilde ~ and exclamation ! operators?

**URL:** https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945
**Category:** New to Julia
**Created:** [April 24, 2022, 4:25pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945 "2022-04-24T16:25:49Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [April 24, 2022, 4:25pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/1 "2022-04-24T16:25:49Z")

</div>

I’m new to Julia and trying to understand when to use `~` and when to use `!` operators. The [documentation](https://docs.julialang.org/en/v1/base/punctuation/) describes each operator as:

> [`!`](https://docs.julialang.org/en/v1/base/math/#Base.:!) an exclamation mark is a prefix operator for logical negation (“not”)  
> [`~`](https://docs.julialang.org/en/v1/base/math/#Base.:~) the tilde is an operator for bitwise not

That reads like the same thing, said two different ways. Okay, so let’s try some things out and see if we can find some distinction between the two operators:

```julia
julia> !true
false

julia> ~true
false

```

Okay, at first glance, they both appear to do exactly the same thing. But when I re-read the definition of the `~`, I see it mentions bitwise operation, so perhaps it works on boolean matrices:

```julia
julia> ~[true true false]
ERROR: MethodError: no method matching ~(::Matrix{Bool})

```

Nope, that didn’t work, because bitwise operation requires a dot before the `~`:

```julia
julia> .~[true true false]
1×3 BitMatrix:
 0 0 1

```

Great, that worked. Now just out of curiosity, I try the same thing with `.!`:

```julia
julia> .![true true false]
1×3 BitMatrix:
 0 0 1

```

So in fact it appears that the “bitwise” distinction in the documentation for `~` shouldn’t suggest that its behavior is any different from `!`.

The definitions for `~` and `~` are almost identical to me, and their behavior appears to be the same, _so what exactly is the difference between the `!` and `~` operators?_

---

<div class="post-metadata">

### Author: ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)
#### Post date: [April 24, 2022, 4:31pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/2 "2022-04-24T16:31:47Z")

</div>

If you check `methods(!)` you’ll see that this one is only defined for three types, namely Bool, Function and Missing

Whereas `~` is defined for a few more, including Bool but also all different Integer types.  
That makes sense, because `~` inverts each bit of the variables on the bit level.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [April 24, 2022, 4:32pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/3 "2022-04-24T16:32:33Z")

</div>

You don’t see the difference between `~` and `!` when you apply it to a single bit (a `Bool`). However, `!` can be applied _only_ to boolean values, while `~` [operates bitwise](https://en.wikipedia.org/wiki/Bitwise_operation) on any integer numbers (including boolean values):

```julia
julia> string(!0b01100110; base=2)
ERROR: MethodError: no method matching !(::UInt8)
Closest candidates are:
  !(::Function) at /usr/share/julia/base/operators.jl:1117
  !(::Bool) at /usr/share/julia/base/bool.jl:35
  !(::Missing) at /usr/share/julia/base/missing.jl:101
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

julia> string(~0b01100110; base=2)
"10011001"

```

---

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [April 24, 2022, 4:37pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/4 "2022-04-24T16:37:07Z")

</div>

Interesting. So is there ever a reason to use `!`?

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [April 24, 2022, 4:37pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/5 "2022-04-24T16:37:42Z")

</div>

When you operate on a boolean value.

---

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [April 24, 2022, 4:38pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/6 "2022-04-24T16:38:25Z")

</div>

When operating on a boolean value, what’s the advantage of `!` over `~`?

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [April 24, 2022, 4:41pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/7 "2022-04-24T16:41:55Z")

</div>

None, they’re identical:

> <https://github.com/JuliaLang/julia/blob/45abec434ae5732cb9fb04f3fa720597c568b448/base/bool.jl#L35-L37>

But the distinction between `!` for logical negation and `~` for [bit twiddling](https://en.wikipedia.org/wiki/Bit_manipulation) is pretty common in many other languages.

---

<div class="post-metadata">

### Author: ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)
#### Post date: [April 24, 2022, 4:43pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/8 "2022-04-24T16:43:44Z")

</div>

The advantage is mostly just semantic, for the person reading your code, as logical and bitwise operations usually have different reasons to be done. The same reason you would write `x÷2` instead of `x>>1` although they are both the same (and the compiler would optimize the first into the second).

---

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [April 24, 2022, 4:46pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/9 "2022-04-24T16:46:32Z")

</div>

That’s very clear, thank you @giordano!

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [April 24, 2022, 7:23pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/10 "2022-04-24T19:23:24Z")

</div>

> [@chadagreene](#):
>
> But when I re-read the definition of the `~` , I see it mentions bitwise operation, so perhaps it works on boolean matrices:

Just a final complement, `bitwise` is different from `element-wise`. The former term is used for the individual bits inside a byte (or any number value/object), the latter is used more generically for any kind of elements inside any kind of container.

---

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [April 24, 2022, 8:15pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/11 "2022-04-24T20:15:53Z")

</div>

Oh, thank you for teaching me this distinction, @Henrique_Becker!

---

<div class="post-metadata">

### Author: ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)
#### Post date: [April 25, 2022, 8:36am UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/12 "2022-04-25T08:36:00Z")

</div>

Precisely speaking that’s not correct, is it?  
The `!` has basically three cases where it can be used.

```julia
julia> methods(!)
# 3 methods for generic function "!":
[1] !(f::Function) in Base at operators.jl:1117
[2] !(x::Bool) in Base at bool.jl:35
[3] !(::Missing) in Base at missing.jl:101

julia> !missing
missing

```

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [April 25, 2022, 11:51pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-the-tilde-and-exclamation-operators/79945/13 "2022-04-25T23:51:18Z")

</div>

Clarity of intent
