# Ampersand Operator not consistent with help?

**URL:** <https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266>\
**Category:** General Usage\
**Created:** [February 6, 2020, 6:46pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266 "2020-02-06T18:46:36Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [February 6, 2020, 6:46pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/1 "2020-02-06T18:46:36Z")

</div>

help?\> &  
search: &

&(x, y)

But if I try it, it doesn’t work:

julia\> &((1==2),(1==1))  
ERROR: syntax: invalid syntax &(Core.tuple(==(1, 2), ==(1, 1)))  
Stacktrace:  
[1] top-level scope at none:0

julia\> &(true,true)  
ERROR: syntax: invalid syntax &(Core.tuple(true, true))  
Stacktrace:  
[1] top-level scope at none:0

I guess I have lost the plot a bit… (I’m used to following ‘what’s on the tin’…)

---

<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:** [February 6, 2020, 6:56pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/2 "2020-02-06T18:56:04Z")

</div>

Yes, that part of the docstring should be fixed (and similarly for `|`). But note that the examples there are correct. I made a PR,

[https://github.com/JuliaLang/julia/pull/34688](https://github.com/JuliaLang/julia/pull/34688)

If you insist on a prefix syntax, use `(&)(true, true)` etc.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [February 6, 2020, 6:58pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/3 "2020-02-06T18:58:24Z")

</div>

Many thanks! Works for me!

You may close this (thanks!)

---

<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:** [February 6, 2020, 7:05pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/4 "2020-02-06T19:05:50Z")

</div>

I could not find an open issue for this (and opening one seemed overkill, compared to just making a PR), so I am not sure what to close here. In any case, this PR should fix the docs.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [February 6, 2020, 7:06pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/5 "2020-02-06T19:06:29Z")

</div>

Great - thanks! [I found out how to mark as ‘solved’]

---

<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:** [February 6, 2020, 7:07pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/6 "2020-02-06T19:07:17Z")

</div>

Also, note that if you find errors like this, it is very easy to make a trivial PR on Github directly, see

> <https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md#improving-documentation>

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [February 6, 2020, 7:09pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/7 "2020-02-06T19:09:58Z")

</div>

I did not report it as an error, as there is so much about Julia that I find counter-intuitive that I just assumed it was me…

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 6, 2020, 8:02pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/8 "2020-02-06T20:02:34Z")

</div>

Interesting: & is bitwise and. && is logical and.

```julia
julia> (&)((1 == 2), (1 == 1))
false

```

and

```julia
julia> (&&)((1 == 2), (1 == 1))
ERROR: syntax: invalid identifier name "&&"
Stacktrace:
 [1] top-level scope at REPL[4]:100:

```

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [February 6, 2020, 8:54pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/9 "2020-02-06T20:54:14Z")

</div>

Now I am confused. I thought ‘&’ was the logical AND(?)

Help says && is the ‘short-circuiting’ logical AND(?). What if I just want  
the ‘garden-variety’ logical AND?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 6, 2020, 9:18pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/10 "2020-02-06T21:18:38Z")

</div>

That’s `&`

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [February 6, 2020, 11:13pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/11 "2020-02-06T23:13:00Z")

</div>

As the documentation says, `&` is the bitwise AND, meaning that it operates bit-to-bit on any pair of integers (plus `missing` values) – unlike `&&`, which only works on `Bool` numbers. For instance:

```julia
julia> 0b110 & 0b101
0x04

```

(with `0x04` being the same as binary `0b100`).

But if you use it on `Bool` numbers, of course `&` will work as a logical AND, without short-circuit.

Yet another difference is that `&` is a broadcastable function, so you can use it with arrays, etc. The short-circuit operator only works with a single pair of logical values.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 6, 2020, 11:17pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/12 "2020-02-06T23:17:50Z")

</div>

Isn’t it weird that Base.:& is a function, but there is no Base.:&& (the boolean short-circuiting operator)? Is it handled as a keyword?

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [February 6, 2020, 11:26pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/13 "2020-02-06T23:26:53Z")

</div>

Yes:  
[https://docs.julialang.org/en/v1/base/math/#&&](https://docs.julialang.org/en/v1/base/math/#&&)

---

<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:** [February 6, 2020, 11:55pm UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/14 "2020-02-06T23:55:18Z")

</div>

Short-circuiting means the second part of the statement is guaranteed to only be evaluated if the first one evaluates as true. This is not something that is possible with regular functions and therefore has to be special cased at a compiler level. Because of that, `&&` and `||` get their own expression types and are not `call` expressions, like most infix operators.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [February 7, 2020, 8:28am UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/15 "2020-02-07T08:28:58Z")

</div>

Is there a reason why `&(a,b)` should be a syntax error when `+(a,b)` is allowed?

Otherwise it would seem more logical to make it work as advertised, rather than adjusting the documentation.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 7, 2020, 10:13am UTC](https://discourse.julialang.org/t/ampersand-operator-not-consistent-with-help/34266/16 "2020-02-07T10:13:07Z")

</div>

`&` was once used as a unary operator in `ccall` and therefore had to be parsed differently. That use was deprecated for Julia 0.7 and according to [deprecate `&x` syntax in ccall, replace with Ref · Issue #6080 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/6080#issuecomment-327029743) the plan was to change the parsing for 1.0 but it seems that didn’t happen. I’ve opened an issue to revisit this: [Parse `&x`as a call expression · Issue #34693 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/34693).
